file transfers fixes - part 7
This commit is contained in:
parent
1b8241eee9
commit
bde69bd417
7 changed files with 32 additions and 15 deletions
|
@ -29,6 +29,7 @@ from messenger.messenger import Messenger
|
|||
from network.tox_dns import ToxDns
|
||||
from history.history import History
|
||||
from file_transfers.file_transfers_messages_service import FileTransfersMessagesService
|
||||
import styles.style # TODO: dynamic loading
|
||||
|
||||
|
||||
class App:
|
||||
|
|
|
@ -18,13 +18,12 @@ class Friend(contact.Contact):
|
|||
# File transfers support
|
||||
# -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
def update_transfer_data(self, file_number, inline): # TODO: rewrite
|
||||
def insert_inline(self, before_message_id, inline):
|
||||
"""
|
||||
Update status of active transfer and load inline if needed
|
||||
"""
|
||||
try:
|
||||
tr = list(filter(lambda x: x.get_type() == MESSAGE_TYPE['FILE_TRANSFER'] and x.is_active(file_number),
|
||||
self._corr))[0]
|
||||
tr = list(filter(lambda x: x.message_id == before_message_id, self._corr))[0]
|
||||
i = self._corr.index(tr)
|
||||
if inline: # inline was loaded
|
||||
self._corr.insert(i, inline)
|
||||
|
|
|
@ -198,6 +198,8 @@ class SendFromBuffer(FileTransfer):
|
|||
def get_data(self):
|
||||
return self._data
|
||||
|
||||
data = property(get_data)
|
||||
|
||||
def send_chunk(self, position, size):
|
||||
if self._creation_time is None:
|
||||
self._creation_time = time()
|
||||
|
@ -289,6 +291,8 @@ class ReceiveToBuffer(FileTransfer):
|
|||
def get_data(self):
|
||||
return self._data
|
||||
|
||||
data = property(get_data)
|
||||
|
||||
def write_chunk(self, position, data):
|
||||
if self._creation_time is None:
|
||||
self._creation_time = time()
|
||||
|
|
|
@ -14,6 +14,8 @@ class FileTransfersHandler:
|
|||
# key = (friend number, file number), value - transfer instance
|
||||
self._paused_file_transfers = dict(settings['paused_file_transfers'])
|
||||
# key - file id, value: [path, friend number, is incoming, start position]
|
||||
self._insert_inline_before = {}
|
||||
# key = (friend number, file number), value - message id
|
||||
|
||||
profile.avatar_changed_event.add_callback(self._send_avatar_to_contacts)
|
||||
|
||||
|
@ -124,6 +126,8 @@ class FileTransfersHandler:
|
|||
rt.set_state_changed_handler(message.transfer_updated)
|
||||
self._file_transfers[(friend_number, file_number)] = rt
|
||||
rt.send_control(TOX_FILE_CONTROL['RESUME'])
|
||||
if inline:
|
||||
self._insert_inline_before[(friend_number, file_number)] = message.message_id
|
||||
|
||||
def send_screenshot(self, data, friend_number):
|
||||
"""
|
||||
|
@ -147,7 +151,7 @@ class FileTransfersHandler:
|
|||
elif friend.status is None and is_resend:
|
||||
raise RuntimeError()
|
||||
st = SendFromBuffer(self._tox, friend.number, data, file_name)
|
||||
self._send_file_add_handlers(st, friend, file_name)
|
||||
self._send_file_add_set_handlers(st, friend, file_name, True)
|
||||
|
||||
def send_file(self, path, friend_number, is_resend=False, file_id=None):
|
||||
"""
|
||||
|
@ -167,7 +171,7 @@ class FileTransfersHandler:
|
|||
raise RuntimeError()
|
||||
st = SendTransfer(path, self._tox, friend_number, TOX_FILE_KIND['DATA'], file_id)
|
||||
file_name = os.path.basename(path)
|
||||
self._send_file_add_handlers(st, friend, file_name)
|
||||
self._send_file_add_set_handlers(st, friend, file_name)
|
||||
|
||||
def incoming_chunk(self, friend_number, file_number, position, data):
|
||||
"""
|
||||
|
@ -188,11 +192,12 @@ 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.get_data())
|
||||
index = self._get_friend_by_number(friend_number).update_transfer_data(file_number, inline)
|
||||
inline = InlineImage(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)
|
||||
self._file_transfers_message_service.add_inline_message(transfer, index)
|
||||
del self._file_transfers[(friend_number, file_number)]
|
||||
del transfer
|
||||
|
||||
def send_files(self, friend_number):
|
||||
friend = self._get_friend_by_number(friend_number)
|
||||
|
@ -268,12 +273,14 @@ class FileTransfersHandler:
|
|||
def _get_all_friends(self):
|
||||
return self._contact_provider.get_all_friends()
|
||||
|
||||
def _send_file_add_handlers(self, st, friend, file_name):
|
||||
def _send_file_add_set_handlers(self, st, friend, file_name, inline=False):
|
||||
st.set_transfer_finished_handler(self.transfer_finished)
|
||||
file_number = st.get_file_number()
|
||||
self._file_transfers[(friend.number, file_number)] = st
|
||||
tm = self._file_transfers_message_service.add_outgoing_transfer_message(friend, st.size, file_name, file_number)
|
||||
st.set_state_changed_handler(tm.transfer_updated)
|
||||
if inline:
|
||||
self._insert_inline_before[(friend.number, file_number)] = tm.message_id
|
||||
|
||||
@staticmethod
|
||||
def _generate_valid_path(path, from_position):
|
||||
|
|
|
@ -195,8 +195,11 @@ def file_recv_chunk(file_transfer_handler):
|
|||
Incoming chunk
|
||||
"""
|
||||
def wrapped(tox, friend_number, file_number, position, chunk, length, user_data):
|
||||
execute(file_transfer_handler.incoming_chunk, friend_number, file_number, position,
|
||||
chunk[:length] if length else None)
|
||||
chunk = chunk[:length] if length else None
|
||||
if length:
|
||||
execute(file_transfer_handler.incoming_chunk, friend_number, file_number, position, chunk)
|
||||
else:
|
||||
invoke_in_main_thread(file_transfer_handler.incoming_chunk, friend_number, file_number, position, chunk)
|
||||
|
||||
return wrapped
|
||||
|
||||
|
@ -206,7 +209,10 @@ def file_chunk_request(file_transfer_handler):
|
|||
Outgoing chunk
|
||||
"""
|
||||
def wrapped(tox, friend_number, file_number, position, size, user_data):
|
||||
execute(file_transfer_handler.outgoing_chunk, friend_number, file_number, position, size)
|
||||
if size:
|
||||
execute(file_transfer_handler.outgoing_chunk, friend_number, file_number, position, size)
|
||||
else:
|
||||
invoke_in_main_thread(file_transfer_handler.outgoing_chunk, friend_number, file_number, position, size)
|
||||
|
||||
return wrapped
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ class MessagesItemsFactory:
|
|||
|
||||
def create_inline_item(self, data, append=True, position=0):
|
||||
elem = QtWidgets.QListWidgetItem()
|
||||
item = InlineImageItem(data, self._messages.width(), elem)
|
||||
item = InlineImageItem(data, self._messages.width(), elem, self._messages)
|
||||
elem.setSizeHint(QtCore.QSize(self._messages.width(), item.height()))
|
||||
if append:
|
||||
self._messages.addItem(elem)
|
||||
|
|
|
@ -406,9 +406,9 @@ class UnsentFileItem(FileTransferItem):
|
|||
|
||||
class InlineImageItem(QtWidgets.QScrollArea):
|
||||
|
||||
def __init__(self, data, width, elem):
|
||||
def __init__(self, data, width, elem, parent=None):
|
||||
|
||||
QtWidgets.QScrollArea.__init__(self)
|
||||
QtWidgets.QScrollArea.__init__(self, parent)
|
||||
self.setFocusPolicy(QtCore.Qt.NoFocus)
|
||||
self._elem = elem
|
||||
self._image_label = QtWidgets.QLabel(self)
|
||||
|
|
Loading…
Reference in a new issue