From 160e9dc72f4a51350b85fcde3b6aeb9150de3a68 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Sun, 8 May 2016 22:29:50 +0300 Subject: [PATCH] file resuming by friend support. file transfers improvements --- src/callbacks.py | 17 ++++++++++++----- src/file_transfers.py | 17 +++++++++++++---- src/list_items.py | 2 +- src/menu.py | 4 ++-- src/messages.py | 4 +++- src/profile.py | 17 ++++++++++++++--- 6 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/callbacks.py b/src/callbacks.py index 8ab78fb..6606ee0 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -183,11 +183,14 @@ def file_recv_chunk(tox, friend_number, file_number, position, chunk, length, us """ Incoming chunk """ - invoke_in_main_thread(Profile.get_instance().incoming_chunk, - friend_number, - file_number, - position, - chunk[:length] if length else None) + if not length: + invoke_in_main_thread(Profile.get_instance().incoming_chunk, + friend_number, + file_number, + position, + None) + else: + Profile.get_instance().incoming_chunk(friend_number, file_number, position,chunk[:length]) def file_chunk_request(tox, friend_number, file_number, position, size, user_data): @@ -210,6 +213,10 @@ def file_recv_control(tox, friend_number, file_number, file_control, user_data): """ if file_control == TOX_FILE_CONTROL['CANCEL']: Profile.get_instance().cancel_transfer(friend_number, file_number, True) + elif file_control == TOX_FILE_CONTROL['PAUSE']: + Profile.get_instance().pause_transfer(friend_number, file_number, True) + elif file_control == TOX_FILE_CONTROL['RESUME']: + Profile.get_instance().resume_transfer(friend_number, file_number, True) # ----------------------------------------------------------------------------------------------------------------- diff --git a/src/file_transfers.py b/src/file_transfers.py index 4dfc945..ce8a99d 100644 --- a/src/file_transfers.py +++ b/src/file_transfers.py @@ -42,6 +42,9 @@ class FileTransfer(QtCore.QObject): def set_state_changed_handler(self, handler): self._state_changed.signal.connect(handler) + def signal(self): + self._state_changed.signal.emit(self.state, self._done / self._size if self._size else 0) + def get_file_number(self): return self._file_number @@ -58,12 +61,18 @@ class FileTransfer(QtCore.QObject): if hasattr(self, '_file'): sleep(0.1) self._file.close() - self._state_changed.signal.emit(TOX_FILE_CONTROL['CANCEL'], 1) + self.state = TOX_FILE_TRANSFER_STATE['CANCELED'] + self._state_changed.signal.emit(self.state, 1) + + def pause(self, by_friend): + if not by_friend: + self.send_control(TOX_FILE_CONTROL['PAUSE']) + self.signal() def send_control(self, control): if self._tox.file_control(self._friend_number, self._file_number, control): self.state = control - self._state_changed.signal.emit(self.state, self._done / self._size if self._size else 0) + self.signal() def get_file_id(self): return self._tox.file_get_file_id(self._friend_number, self._file_number) @@ -96,7 +105,7 @@ class SendTransfer(FileTransfer): data = self._file.read(size) self._tox.file_send_chunk(self._friend_number, self._file_number, position, data) self._done += size - self._state_changed.signal.emit(self.state, self._done / self._size) + self.signal() else: if hasattr(self, '_file'): self._file.close() @@ -209,7 +218,7 @@ class ReceiveToBuffer(FileTransfer): if position + l > self._data_size: self._data_size = position + l self._done += l - self._state_changed.signal.emit(self.state, self._done / self._size) + self.signal() class ReceiveAvatar(ReceiveTransfer): diff --git a/src/list_items.py b/src/list_items.py index 7d1ab3a..f60f4ef 100644 --- a/src/list_items.py +++ b/src/list_items.py @@ -192,7 +192,7 @@ class FileTransferItem(QtGui.QListWidget): self.pb.setVisible(False) self.file_name = DataLabel(self) - self.file_name.setGeometry(QtCore.QRect(210, 2, 230, 46)) + self.file_name.setGeometry(QtCore.QRect(210, 2, width - 400, 46)) font.setPointSize(12) self.file_name.setFont(font) file_size = size / 1024 diff --git a/src/menu.py b/src/menu.py index a61a80a..886ba05 100644 --- a/src/menu.py +++ b/src/menu.py @@ -210,7 +210,7 @@ class NetworkSettings(CenteredWidget): settings = Settings.get_instance() self.ipv.setChecked(settings['ipv6_enabled']) self.udp.setChecked(settings['udp_enabled']) - self.proxy.setChecked(settings['proxy_type'] != 0) + self.proxy.setChecked(settings['proxy_type']) self.proxyip.setText(settings['proxy_host']) self.proxyport.setText(unicode(settings['proxy_port'])) self.http.setChecked(settings['proxy_type'] == 1) @@ -245,7 +245,7 @@ class NetworkSettings(CenteredWidget): if changed: settings['ipv6_enabled'] = self.ipv.isChecked() settings['udp_enabled'] = self.udp.isChecked() - settings['proxy_type'] = 2 - int(self.http.isChecked()) + settings['proxy_type'] = 2 - int(self.http.isChecked()) if self.proxy.isChecked() else 0 settings['proxy_host'] = str(self.proxyip.text()) settings['proxy_port'] = int(self.proxyport.text()) settings.save() diff --git a/src/messages.py b/src/messages.py index 712bccb..2d45ce9 100644 --- a/src/messages.py +++ b/src/messages.py @@ -12,7 +12,9 @@ FILE_TRANSFER_MESSAGE_STATUS = { 'CANCELLED': 1, 'OUTGOING': 2, 'INCOMING_NOT_STARTED': 3, - 'INCOMING_STARTED': 4 + 'INCOMING_STARTED': 4, + 'PAUSED_BY_FRIEND': 5, + 'PAUSED_BY_USER': 6 } diff --git a/src/profile.py b/src/profile.py index 097eb33..712bdf5 100644 --- a/src/profile.py +++ b/src/profile.py @@ -207,7 +207,7 @@ class Friend(Contact): """ if hasattr(self, '_message_getter'): del self._message_getter - self._corr = filter(lambda x: x.get_type() == 2 and x.get_status() in (2, 3, 4), self._corr) + self._corr = filter(lambda x: x.get_type() == 2 and x.get_status() >= 2, self._corr) self._unsaved_messages = 0 def update_transfer_data(self, file_number, status, inline=None): @@ -419,9 +419,10 @@ class Profile(Contact, Singleton): data[3]) elif message.get_type() == 2: item = self.create_file_transfer_item(message) - if message.get_status() in (2, 4): # active file transfer + if message.get_status() >= 2: # active file transfer ft = self._file_transfers[(message.get_friend_number(), message.get_file_number())] ft.set_state_changed_handler(item.update) + ft.signal() else: # inline self.create_inline_item(message.get_data()) self._messages.scrollToBottom() @@ -611,7 +612,7 @@ class Profile(Contact, Singleton): False) elif message.get_type() == 2: item = self.create_file_transfer_item(message, False) - if message.get_status() in (2, 4): + if message.get_status() >= 2: ft = self._file_transfers[(message.get_friend_number(), message.get_file_number())] ft.set_state_changed_handler(item.update) @@ -940,6 +941,16 @@ class Profile(Contact, Singleton): else: self._tox.file_control(friend_number, file_number, TOX_FILE_CONTROL['CANCEL']) + def pause_transfer(self, friend_number, file_number, by_friend=False): + tr = self._file_transfers[(friend_number, file_number)] + tr.pause(by_friend) + t = FILE_TRANSFER_MESSAGE_STATUS['PAUSED_BY_FRIEND'] if by_friend else FILE_TRANSFER_MESSAGE_STATUS['PAUSED_BY_USER'] + self.get_friend_by_number(friend_number).update_transfer_data(file_number, t) + + def resume_transfer(self, friend_number, file_number, by_friend=False): + self.get_friend_by_number(friend_number).update_transfer_data(file_number, + FILE_TRANSFER_MESSAGE_STATUS['OUTGOING']) + def accept_transfer(self, item, path, friend_number, file_number, size, inline=False): """ :param item: transfer item.