file resuming by friend support. file transfers improvements

This commit is contained in:
ingvar1995 2016-05-08 22:29:50 +03:00
parent c503cf4a72
commit 160e9dc72f
6 changed files with 45 additions and 16 deletions

View file

@ -183,11 +183,14 @@ def file_recv_chunk(tox, friend_number, file_number, position, chunk, length, us
""" """
Incoming chunk Incoming chunk
""" """
invoke_in_main_thread(Profile.get_instance().incoming_chunk, if not length:
friend_number, invoke_in_main_thread(Profile.get_instance().incoming_chunk,
file_number, friend_number,
position, file_number,
chunk[:length] if length else None) 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): 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']: if file_control == TOX_FILE_CONTROL['CANCEL']:
Profile.get_instance().cancel_transfer(friend_number, file_number, True) 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)
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------

View file

@ -42,6 +42,9 @@ class FileTransfer(QtCore.QObject):
def set_state_changed_handler(self, handler): def set_state_changed_handler(self, handler):
self._state_changed.signal.connect(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): def get_file_number(self):
return self._file_number return self._file_number
@ -58,12 +61,18 @@ class FileTransfer(QtCore.QObject):
if hasattr(self, '_file'): if hasattr(self, '_file'):
sleep(0.1) sleep(0.1)
self._file.close() 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): def send_control(self, control):
if self._tox.file_control(self._friend_number, self._file_number, control): if self._tox.file_control(self._friend_number, self._file_number, control):
self.state = 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): def get_file_id(self):
return self._tox.file_get_file_id(self._friend_number, self._file_number) 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) data = self._file.read(size)
self._tox.file_send_chunk(self._friend_number, self._file_number, position, data) self._tox.file_send_chunk(self._friend_number, self._file_number, position, data)
self._done += size self._done += size
self._state_changed.signal.emit(self.state, self._done / self._size) self.signal()
else: else:
if hasattr(self, '_file'): if hasattr(self, '_file'):
self._file.close() self._file.close()
@ -209,7 +218,7 @@ class ReceiveToBuffer(FileTransfer):
if position + l > self._data_size: if position + l > self._data_size:
self._data_size = position + l self._data_size = position + l
self._done += l self._done += l
self._state_changed.signal.emit(self.state, self._done / self._size) self.signal()
class ReceiveAvatar(ReceiveTransfer): class ReceiveAvatar(ReceiveTransfer):

View file

@ -192,7 +192,7 @@ class FileTransferItem(QtGui.QListWidget):
self.pb.setVisible(False) self.pb.setVisible(False)
self.file_name = DataLabel(self) 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) font.setPointSize(12)
self.file_name.setFont(font) self.file_name.setFont(font)
file_size = size / 1024 file_size = size / 1024

View file

@ -210,7 +210,7 @@ class NetworkSettings(CenteredWidget):
settings = Settings.get_instance() settings = Settings.get_instance()
self.ipv.setChecked(settings['ipv6_enabled']) self.ipv.setChecked(settings['ipv6_enabled'])
self.udp.setChecked(settings['udp_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.proxyip.setText(settings['proxy_host'])
self.proxyport.setText(unicode(settings['proxy_port'])) self.proxyport.setText(unicode(settings['proxy_port']))
self.http.setChecked(settings['proxy_type'] == 1) self.http.setChecked(settings['proxy_type'] == 1)
@ -245,7 +245,7 @@ class NetworkSettings(CenteredWidget):
if changed: if changed:
settings['ipv6_enabled'] = self.ipv.isChecked() settings['ipv6_enabled'] = self.ipv.isChecked()
settings['udp_enabled'] = self.udp.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_host'] = str(self.proxyip.text())
settings['proxy_port'] = int(self.proxyport.text()) settings['proxy_port'] = int(self.proxyport.text())
settings.save() settings.save()

View file

@ -12,7 +12,9 @@ FILE_TRANSFER_MESSAGE_STATUS = {
'CANCELLED': 1, 'CANCELLED': 1,
'OUTGOING': 2, 'OUTGOING': 2,
'INCOMING_NOT_STARTED': 3, 'INCOMING_NOT_STARTED': 3,
'INCOMING_STARTED': 4 'INCOMING_STARTED': 4,
'PAUSED_BY_FRIEND': 5,
'PAUSED_BY_USER': 6
} }

View file

@ -207,7 +207,7 @@ class Friend(Contact):
""" """
if hasattr(self, '_message_getter'): if hasattr(self, '_message_getter'):
del 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 self._unsaved_messages = 0
def update_transfer_data(self, file_number, status, inline=None): def update_transfer_data(self, file_number, status, inline=None):
@ -419,9 +419,10 @@ class Profile(Contact, Singleton):
data[3]) data[3])
elif message.get_type() == 2: elif message.get_type() == 2:
item = self.create_file_transfer_item(message) 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 = self._file_transfers[(message.get_friend_number(), message.get_file_number())]
ft.set_state_changed_handler(item.update) ft.set_state_changed_handler(item.update)
ft.signal()
else: # inline else: # inline
self.create_inline_item(message.get_data()) self.create_inline_item(message.get_data())
self._messages.scrollToBottom() self._messages.scrollToBottom()
@ -611,7 +612,7 @@ class Profile(Contact, Singleton):
False) False)
elif message.get_type() == 2: elif message.get_type() == 2:
item = self.create_file_transfer_item(message, False) 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 = self._file_transfers[(message.get_friend_number(), message.get_file_number())]
ft.set_state_changed_handler(item.update) ft.set_state_changed_handler(item.update)
@ -940,6 +941,16 @@ class Profile(Contact, Singleton):
else: else:
self._tox.file_control(friend_number, file_number, TOX_FILE_CONTROL['CANCEL']) 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): def accept_transfer(self, item, path, friend_number, file_number, size, inline=False):
""" """
:param item: transfer item. :param item: transfer item.