Fixed crash on reconnection, file transfers fixes

This commit is contained in:
ingvar1995 2018-09-24 22:06:30 +03:00
parent ae4eae92ae
commit cf4cfa979c
7 changed files with 37 additions and 32 deletions

View file

@ -116,7 +116,7 @@ class ContactsManager(ToxSave):
if message.type == MESSAGE_TYPE['FILE_TRANSFER']:
self._messages_items_factory.create_file_transfer_item(message)
elif message.type == MESSAGE_TYPE['INLINE']:
self._messages_items_factory.create_inline_item(message.data)
self._messages_items_factory.create_inline_item(message)
else:
self._messages_items_factory.create_message_item(message)
self._messages.scrollToBottom()

View file

@ -2,6 +2,7 @@ from contacts import basecontact
import random
import threading
import common.tox_save as tox_save
from middleware.threads import invoke_in_main_thread
class Profile(basecontact.BaseContact, tox_save.ToxSave):
@ -73,12 +74,13 @@ class Profile(basecontact.BaseContact, tox_save.ToxSave):
Recreate tox instance
"""
self.status = None
self._reset_action()
invoke_in_main_thread(self._reset_action)
def _reconnect(self):
self._waiting_for_reconnection = False
contacts = self._contacts_provider.get_all_friends()
if self.status is None or (all(list(map(lambda x: x.status is None, contacts))) and len(contacts)):
all_friends_offline = all(list(map(lambda x: x.status is None, contacts)))
if self.status is None or (all_friends_offline and len(contacts)):
self._waiting_for_reconnection = True
self.restart()
self._timer = threading.Timer(50, self._reconnect)

View file

@ -52,10 +52,10 @@ class FileTransfer:
self._file_id = self._file = None
def set_state_changed_handler(self, handler):
self._state_changed_event += handler
self._state_changed_event += lambda *args: invoke_in_main_thread(handler, *args)
def set_transfer_finished_handler(self, handler):
self._finished_event += handler
self._finished_event += lambda *args: invoke_in_main_thread(handler, *args)
def get_file_number(self):
return self._file_number
@ -100,17 +100,17 @@ class FileTransfer:
def cancelled(self):
if self._file is not None:
self._file.close()
self.state = FILE_TRANSFER_STATE['CANCELLED']
self.set_state(FILE_TRANSFER_STATE['CANCELLED'])
def pause(self, by_friend):
if not by_friend:
self.send_control(TOX_FILE_CONTROL['PAUSE'])
else:
self.state = FILE_TRANSFER_STATE['PAUSED_BY_FRIEND']
self.set_state(FILE_TRANSFER_STATE['PAUSED_BY_FRIEND'])
def send_control(self, control):
if self._tox.file_control(self._friend_number, self._file_number, control):
self.state = control
self.set_state(control)
def get_file_id(self):
return self._tox.file_get_file_id(self._friend_number, self._file_number)
@ -121,10 +121,10 @@ class FileTransfer:
t = -1
else:
t = ((time() - self._creation_time) / percentage) * (1 - percentage)
invoke_in_main_thread(self._state_changed_event, self.state, percentage, int(t))
self._state_changed_event(self.state, percentage, int(t))
def _finished(self):
invoke_in_main_thread(self._finished_event, self._friend_number, self._file_number)
self._finished_event(self._friend_number, self._file_number)
# -----------------------------------------------------------------------------------------------------------------
# Send file

View file

@ -167,7 +167,7 @@ class FileTransfersHandler(ToxSave):
return
elif friend.status is None and is_resend:
print('Error in sending')
raise RuntimeError()
return
st = SendTransfer(path, self._tox, friend_number, TOX_FILE_KIND['DATA'], file_id)
file_name = os.path.basename(path)
self._send_file_add_set_handlers(st, friend, file_name)
@ -210,7 +210,7 @@ class FileTransfersHandler(ToxSave):
else:
self.send_file(path, friend_number, True)
friend.clear_unsent_files()
for key in list(self._paused_file_transfers.keys()):
for key in self._paused_file_transfers.keys():
(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]
@ -221,14 +221,15 @@ class FileTransfersHandler(ToxSave):
print('Exception in file sending: ' + str(ex))
def friend_exit(self, friend_number):
for friend_num, file_num in list(self._file_transfers.keys()):
if friend_num == friend_number:
ft = self._file_transfers[(friend_num, file_num)]
if type(ft) is SendTransfer:
self._paused_file_transfers[ft.file_id] = [ft.path, friend_num, False, -1]
elif type(ft) is ReceiveTransfer and ft.state != FILE_TRANSFER_STATE['INCOMING_NOT_STARTED']:
self._paused_file_transfers[ft.file_id] = [ft.path, friend_num, True, ft.total_size()]
self.cancel_transfer(friend_num, file_num, True)
for friend_num, file_num in self._file_transfers.keys():
if friend_num != friend_number:
continue
ft = self._file_transfers[(friend_num, file_num)]
if type(ft) is SendTransfer:
self._paused_file_transfers[ft.file_id] = [ft.path, friend_num, False, -1]
elif type(ft) is ReceiveTransfer and ft.state != FILE_TRANSFER_STATE['INCOMING_NOT_STARTED']:
self._paused_file_transfers[ft.file_id] = [ft.path, friend_num, True, ft.total_size()]
self.cancel_transfer(friend_num, file_num, True)
# -----------------------------------------------------------------------------------------------------------------
# Avatars support
@ -267,7 +268,9 @@ class FileTransfersHandler(ToxSave):
# -----------------------------------------------------------------------------------------------------------------
def _is_friend_online(self, friend_number):
return self._get_friend_by_number(friend_number).status is not None
friend = self._get_friend_by_number(friend_number)
return friend.status is not None
def _get_friend_by_number(self, friend_number):
return self._contact_provider.get_friend_by_number(friend_number)

View file

@ -82,14 +82,15 @@ class History:
messages.reverse()
messages = messages[self._messages.count():self._messages.count() + PAGE_SIZE]
for message in messages:
if message.get_type() in (MESSAGE_TYPE['TEXT'], MESSAGE_TYPE['ACTION']): # text message
message_type = message.get_type()
if message_type in (MESSAGE_TYPE['TEXT'], 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:
elif message_type == MESSAGE_TYPE['FILE_TRANSFER']: # file transfer
if message.state == FILE_TRANSFER_STATE['UNSENT']:
self._create_unsent_file_item(message)
continue
self._create_file_transfer_item(message)
elif message.get_type() == MESSAGE_TYPE['INLINE']: # inline image
else:
self._create_file_transfer_item(message)
elif message_type == MESSAGE_TYPE['INLINE']: # inline image
self._create_inline_item(message)
else: # info message
self._create_message_item(message)

View file

@ -183,7 +183,7 @@ def tox_file_recv(window, tray, profile, file_transfer_handler, contacts_manager
sound_notification(SOUND_NOTIFICATION['FILE_TRANSFER'])
icon = util.join_path(util.get_images_directory(), 'icon_new_messages.png')
invoke_in_main_thread(tray.setIcon, QtGui.QIcon(icon))
else: # AVATAR
else: # avatar
print('Avatar')
invoke_in_main_thread(file_transfer_handler.incoming_avatar,
friend_number,
@ -208,7 +208,7 @@ def file_chunk_request(file_transfer_handler):
Outgoing chunk
"""
def wrapped(tox, friend_number, file_number, position, size, user_data):
invoke_in_main_thread(file_transfer_handler.outgoing_chunk, friend_number, file_number, position, size)
execute(file_transfer_handler.outgoing_chunk, friend_number, file_number, position, size)
return wrapped
@ -440,7 +440,6 @@ def group_peer_join(contacts_provider, groups_service):
def group_peer_exit(contacts_provider, groups_service, contacts_manager):
def wrapped(tox, group_number, peer_id, message, length, user_data):
group = contacts_provider.get_group_by_number(group_number)
contacts_manager.remove_group_peer_by_id(group, peer_id)
group.remove_peer(peer_id)
invoke_in_main_thread(groups_service.generate_peers_list)

View file

@ -45,9 +45,9 @@ class MessagesItemsFactory:
return item
def create_inline_item(self, data, append=True, position=0):
def create_inline_item(self, message, append=True, position=0):
elem = QtWidgets.QListWidgetItem()
item = InlineImageItem(data, self._messages.width(), elem, self._messages)
item = InlineImageItem(message.data, self._messages.width(), elem, self._messages)
elem.setSizeHint(QtCore.QSize(self._messages.width(), item.height()))
if append:
self._messages.addItem(elem)