From 6a2bc1f65092307b50e3da760991a5a168f85f24 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Sat, 19 Mar 2016 23:36:54 +0300 Subject: [PATCH] file transfer item update #3. bug fixes --- src/callbacks.py | 1 + src/file_transfers.py | 12 +++++++----- src/list_items.py | 15 ++++++++++++--- src/profile.py | 24 +++++++++++++----------- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/callbacks.py b/src/callbacks.py index 496b72d..d69e614 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -177,6 +177,7 @@ def file_chunk_request(tox, friend_number, file_number, position, size, user_dat def file_recv_control(tox, friend_number, file_number, file_control, user_data): + # TODO: process pass # ----------------------------------------------------------------------------------------------------------------- diff --git a/src/file_transfers.py b/src/file_transfers.py index 252afb8..7389722 100644 --- a/src/file_transfers.py +++ b/src/file_transfers.py @@ -16,7 +16,7 @@ TOX_FILE_TRANSFER_STATE = { class Signal(QtCore.QObject): - signal = QtCore.Signal(int) + signal = QtCore.Signal(str) class FileTransfer(QtCore.QObject): @@ -46,7 +46,7 @@ class FileTransfer(QtCore.QObject): def cancel(self): self.send_control(TOX_FILE_CONTROL['CANCEL']) self._file.close() - self._signal.signal.emit(1) # other signal params - status and %? + self._signal.signal.emit('{} {}'.format(self.state, 0)) def send_control(self, control): if self._tox.file_control(self._friend_number, self._file_number, control): @@ -76,11 +76,12 @@ class SendTransfer(FileTransfer): if size: self._file.seek(position) data = self._file.read(size) - return 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._signal.signal.emit('{} {}'.format(self.state, 0)) else: self._file.close() self.state = TOX_FILE_TRANSFER_STATE['FINISHED'] - self._signal.signal.emit(position) + self._signal.signal.emit('{} {}'.format(self.state, 100)) class SendAvatar(SendTransfer): @@ -117,10 +118,11 @@ class ReceiveTransfer(FileTransfer): self._file.flush() if position + len(data) > self._size: self._size = position + len(data) + self._signal.signal.emit('{} {}'.format(self.state, 0)) else: self._file.close() self.state = TOX_FILE_TRANSFER_STATE['FINISHED'] - self._signal.signal.emit(position) + self._signal.signal.emit('{} {}'.format(self.state, 100)) class ReceiveAvatar(ReceiveTransfer): diff --git a/src/list_items.py b/src/list_items.py index 47ad6c7..ae5724e 100644 --- a/src/list_items.py +++ b/src/list_items.py @@ -1,6 +1,7 @@ from toxcore_enums_and_consts import * from PySide import QtGui, QtCore import profile +from file_transfers import TOX_FILE_TRANSFER_STATE from util import curr_directory @@ -215,10 +216,10 @@ class FileTransferItem(QtGui.QListWidget): def cancel_transfer(self, friend_number, file_number): pr = profile.Profile.get_instance() pr.cancel_transfer(friend_number, file_number) - self.name.setText('Cancelled') self.setStyleSheet('QListWidget { background-color: red; }') self.cancel.setVisible(False) self.accept.setVisible(False) + self.pb.setVisible(False) def accept_transfer(self, friend_number, file_number): directory = QtGui.QFileDialog.getExistingDirectory() @@ -229,6 +230,14 @@ class FileTransferItem(QtGui.QListWidget): @QtCore.Slot(str) def update(self, data): - # TODO: add params - status and % - self.name.setText('Finished') + arr = data.split() + self.pb.setValue(int(arr[1])) + if int(arr[0]) == TOX_FILE_TRANSFER_STATE['CANCELED']: + self.setStyleSheet('QListWidget { background-color: red; }') + self.cancel.setVisible(False) + self.accept.setVisible(False) + self.pb.setVisible(False) + elif int(arr[0]) == TOX_FILE_TRANSFER_STATE['FINISHED']: + self.pb.setVisible(False) + self.cancel.setVisible(False) print data diff --git a/src/profile.py b/src/profile.py index be8a81a..72a1511 100644 --- a/src/profile.py +++ b/src/profile.py @@ -749,12 +749,13 @@ class Profile(Contact, Singleton): self.get_friend_by_number(friend_number).load_avatar() def incoming_chunk(self, friend_number, file_number, position, data): - transfer = self._file_transfers[(friend_number, file_number)] - transfer.write_chunk(position, data) - if transfer.state: - if type(transfer) is ReceiveAvatar: - self.get_friend_by_number(friend_number).load_avatar() - del self._file_transfers[(friend_number, file_number)] + if (friend_number, file_number) in self._file_transfers: + transfer = self._file_transfers[(friend_number, file_number)] + transfer.write_chunk(position, data) + if transfer.state: + if type(transfer) is ReceiveAvatar: + self.get_friend_by_number(friend_number).load_avatar() + del self._file_transfers[(friend_number, file_number)] def send_avatar(self, friend_number): avatar_path = (ProfileHelper.get_path() + 'avatars/{}.png').format(self._tox_id[:TOX_PUBLIC_KEY_SIZE * 2]) @@ -767,14 +768,15 @@ class Profile(Contact, Singleton): friend_number = self.get_active_number() st = SendTransfer(path, self._tox, friend_number) self._file_transfers[(friend_number, st.get_file_number())] = st - item = self.create_file_transfer_item('Out file', friend_number, st.get_file_number(), False) + item = self.create_file_transfer_item(os.path.basename(path), os.path.getsize(path), friend_number, st.get_file_number(), False) st.set_event_handler(item.update) def outgoing_chunk(self, friend_number, file_number, position, size): - transfer = self._file_transfers[(friend_number, file_number)] - transfer.send_chunk(position, size) - if transfer.state: - del self._file_transfers[(friend_number, file_number)] + if (friend_number, file_number) in self._file_transfers: + transfer = self._file_transfers[(friend_number, file_number)] + transfer.send_chunk(position, size) + if transfer.state: + del self._file_transfers[(friend_number, file_number)] def reset_avatar(self): super(Profile, self).reset_avatar()