file transfer item #2. data binding using signals. some other updates

This commit is contained in:
ingvar1995 2016-03-19 20:58:42 +03:00
parent 42ca5d0597
commit 01803a6376
3 changed files with 92 additions and 38 deletions

View file

@ -15,8 +15,11 @@ TOX_FILE_TRANSFER_STATE = {
} }
class Signal(QtCore.QObject):
signal = QtCore.Signal(int)
class FileTransfer(QtCore.QObject): class FileTransfer(QtCore.QObject):
signal = QtCore.Signal()
def __init__(self, path, tox, friend_number, file_number=None): def __init__(self, path, tox, friend_number, file_number=None):
QtCore.QObject.__init__(self) QtCore.QObject.__init__(self)
@ -26,12 +29,13 @@ class FileTransfer(QtCore.QObject):
self.state = TOX_FILE_TRANSFER_STATE['RUNNING'] self.state = TOX_FILE_TRANSFER_STATE['RUNNING']
self._file_number = file_number self._file_number = file_number
self._creation_time = time() self._creation_time = time()
self._signal = Signal()
def set_tox(self, tox): def set_tox(self, tox):
self._tox = tox self._tox = tox
def set_event_handler(self, handler): def set_event_handler(self, handler):
self.signal.connect(handler) self._signal.signal.connect(handler)
def get_file_number(self): def get_file_number(self):
return self._file_number return self._file_number
@ -39,6 +43,11 @@ class FileTransfer(QtCore.QObject):
def get_friend_number(self): def get_friend_number(self):
return self._friend_number return self._friend_number
def cancel(self):
self.send_control(TOX_FILE_CONTROL['CANCEL'])
self._file.close()
self._signal.signal.emit(1) # other signal params - status and %?
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
@ -52,6 +61,7 @@ class FileTransfer(QtCore.QObject):
class SendTransfer(FileTransfer): class SendTransfer(FileTransfer):
def __init__(self, path, tox, friend_number, kind=TOX_FILE_KIND['DATA'], file_id=None): def __init__(self, path, tox, friend_number, kind=TOX_FILE_KIND['DATA'], file_id=None):
super(SendTransfer, self).__init__(path, tox, friend_number) super(SendTransfer, self).__init__(path, tox, friend_number)
self._file_number = tox.file_send(friend_number, self._file_number = tox.file_send(friend_number,
@ -70,10 +80,11 @@ class SendTransfer(FileTransfer):
else: else:
self._file.close() self._file.close()
self.state = TOX_FILE_TRANSFER_STATE['FINISHED'] self.state = TOX_FILE_TRANSFER_STATE['FINISHED']
self.signal.emit() self._signal.signal.emit(position)
class SendAvatar(SendTransfer): class SendAvatar(SendTransfer):
def __init__(self, path, tox, friend_number): def __init__(self, path, tox, friend_number):
if path is None: if path is None:
super(SendAvatar, self).__init__(path, tox, friend_number, TOX_FILE_KIND['AVATAR']) super(SendAvatar, self).__init__(path, tox, friend_number, TOX_FILE_KIND['AVATAR'])
@ -84,13 +95,17 @@ class SendAvatar(SendTransfer):
class ReceiveTransfer(FileTransfer): class ReceiveTransfer(FileTransfer):
# TODO: remove file on cancel and close file
def __init__(self, path, tox, friend_number, file_number): def __init__(self, path, tox, friend_number, file_number):
super(ReceiveTransfer, self).__init__(path, tox, friend_number, file_number) super(ReceiveTransfer, self).__init__(path, tox, friend_number, file_number)
self._file = open(self._path, 'wb') self._file = open(self._path, 'wb')
self._file.truncate(0) self._file.truncate(0)
self._size = 0 self._size = 0
def cancel(self):
super(ReceiveTransfer, self).cancel()
remove(self._path)
def write_chunk(self, position, data): def write_chunk(self, position, data):
if data is not None: if data is not None:
data = ''.join(chr(x) for x in data) data = ''.join(chr(x) for x in data)
@ -105,9 +120,11 @@ class ReceiveTransfer(FileTransfer):
else: else:
self._file.close() self._file.close()
self.state = TOX_FILE_TRANSFER_STATE['FINISHED'] self.state = TOX_FILE_TRANSFER_STATE['FINISHED']
self._signal.signal.emit(position)
class ReceiveAvatar(ReceiveTransfer): class ReceiveAvatar(ReceiveTransfer):
def __init__(self, tox, friend_number, file_number, has_size=True): def __init__(self, tox, friend_number, file_number, has_size=True):
path = profile.ProfileHelper.get_path() + '/avatars/{}.png'.format(tox.friend_get_public_key(friend_number)) path = profile.ProfileHelper.get_path() + '/avatars/{}.png'.format(tox.friend_get_public_key(friend_number))
super(ReceiveAvatar, self).__init__(path, tox, friend_number, file_number) super(ReceiveAvatar, self).__init__(path, tox, friend_number, file_number)

View file

@ -1,5 +1,7 @@
from toxcore_enums_and_consts import * from toxcore_enums_and_consts import *
from PySide import QtGui, QtCore from PySide import QtGui, QtCore
import profile
from util import curr_directory
class MessageEdit(QtGui.QPlainTextEdit): class MessageEdit(QtGui.QPlainTextEdit):
@ -157,11 +159,13 @@ class StatusCircle(QtGui.QWidget):
class FileTransferItem(QtGui.QListWidget): class FileTransferItem(QtGui.QListWidget):
# TODO: accept button def __init__(self, file_name, size, time, user, friend_number, file_number, is_incoming_transfer, parent=None):
def __init__(self, file_name, time, user, friend_number, file_number, parent=None):
QtGui.QListWidget.__init__(self, parent) QtGui.QListWidget.__init__(self, parent)
self.resize(QtCore.QSize(600, 50))
self.setStyleSheet('QListWidget { background-color: green; }')
self.name = QtGui.QLabel(self) self.name = QtGui.QLabel(self)
self.name.setGeometry(QtCore.QRect(0, 2, 95, 20)) self.name.setGeometry(QtCore.QRect(0, 15, 95, 20))
self.name.setTextFormat(QtCore.Qt.PlainText) self.name.setTextFormat(QtCore.Qt.PlainText)
font = QtGui.QFont() font = QtGui.QFont()
font.setFamily("Times New Roman") font.setFamily("Times New Roman")
@ -169,28 +173,62 @@ class FileTransferItem(QtGui.QListWidget):
font.setBold(True) font.setBold(True)
self.name.setFont(font) self.name.setFont(font)
self.name.setObjectName("name") self.name.setObjectName("name")
self.name.setText(file_name) self.name.setText(user)
self.name.setStyleSheet('QLabel { color: black; }')
self.time = QtGui.QLabel(self) self.time = QtGui.QLabel(self)
self.time.setGeometry(QtCore.QRect(500, 0, 50, 25)) self.time.setGeometry(QtCore.QRect(550, 0, 50, 50))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10) font.setPointSize(10)
font.setBold(False) font.setBold(False)
self.time.setFont(font) self.time.setFont(font)
self.time.setObjectName("time") self.time.setObjectName("time")
self.time.setText(time) self.time.setText(time)
self.time.setStyleSheet('QLabel { color: black; }')
self.cancel = QtGui.QPushButton(self) self.cancel = QtGui.QPushButton(self)
self.cancel.setGeometry(QtCore.QRect(100, 2, 200, 20)) self.cancel.setGeometry(QtCore.QRect(500, 0, 50, 50))
self.cancel.setText("Cancel") self.cancel.setText("Cancel")
self.cancel.clicked.connect(lambda: self.click(friend_number, file_number)) self.cancel.clicked.connect(lambda: self.cancel_transfer(friend_number, file_number))
def click(self, friend_number, file_number): self.accept = QtGui.QPushButton(self)
from profile import Profile self.accept.setGeometry(QtCore.QRect(450, 0, 50, 50))
profile = Profile.get_instance() self.accept.setText("Accept")
profile.cancel_transfer(friend_number, file_number) self.accept.clicked.connect(lambda: self.accept_transfer(friend_number, file_number))
self.accept.setVisible(is_incoming_transfer)
self.pb = QtGui.QProgressBar(self)
self.pb.setGeometry(QtCore.QRect(100, 15, 100, 20))
self.pb.setValue(0)
self.file_name = QtGui.QLabel(self)
self.file_name.setGeometry(QtCore.QRect(210, 0, 230, 50))
font.setPointSize(12)
self.file_name.setFont(font)
self.file_name.setObjectName("time")
size /= 1024
if not size:
size = '<1'
self.file_name.setText('{}KB {}'.format(size, file_name))
self.file_name.setStyleSheet('QLabel { color: black; }')
self.saved_name = file_name
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.name.setText('Cancelled')
self.setStyleSheet('QListWidget { background-color: red; }')
self.cancel.setVisible(False)
self.accept.setVisible(False)
def update(self): def accept_transfer(self, friend_number, file_number):
directory = QtGui.QFileDialog.getExistingDirectory()
if directory:
pr = profile.Profile.get_instance()
pr.accept_transfer(self, directory + '/' + self.saved_name, friend_number, file_number)
self.accept.setVisible(False)
@QtCore.Slot(str)
def update(self, data):
# TODO: add params - status and %
self.name.setText('Finished') self.name.setText('Finished')
print data

View file

@ -168,13 +168,13 @@ class Contact(object):
f.write(avatar) f.write(avatar)
self.load_avatar() self.load_avatar()
def get_avatar_hash(self): # def get_avatar_hash(self):
avatar_path = (ProfileHelper.get_path() + 'avatars/{}.png').format(self._tox_id[:TOX_PUBLIC_KEY_SIZE * 2]) # avatar_path = (ProfileHelper.get_path() + 'avatars/{}.png').format(self._tox_id[:TOX_PUBLIC_KEY_SIZE * 2])
if not os.path.isfile(avatar_path): # load default image # if not os.path.isfile(avatar_path): # load default image
return 0 # return 0
with open(avatar_path, 'rb') as fl: # with open(avatar_path, 'rb') as fl:
data = fl.read() # data = fl.read()
return Tox.hash(data) # return Tox.hash(data)
class Friend(Contact): class Friend(Contact):
@ -575,15 +575,16 @@ class Profile(Contact, Singleton):
def create_message_item(self, text, time, name, message_type): def create_message_item(self, text, time, name, message_type):
item = MessageItem(text, time, name, message_type, self._messages) item = MessageItem(text, time, name, message_type, self._messages)
elem = QtGui.QListWidgetItem(self._messages) elem = QtGui.QListWidgetItem(self._messages)
elem.setSizeHint(QtCore.QSize(500, item.getHeight())) elem.setSizeHint(QtCore.QSize(600, item.getHeight()))
self._messages.addItem(elem) self._messages.addItem(elem)
self._messages.setItemWidget(elem, item) self._messages.setItemWidget(elem, item)
self._messages.repaint() self._messages.repaint()
def create_file_transfer_item(self, file_name, friend_number, file_number): def create_file_transfer_item(self, file_name, size, friend_number, file_number, is_incoming_transfer):
item = FileTransferItem(file_name, curr_time(), '', friend_number, file_number) friend = self.get_friend_by_number(friend_number)
item = FileTransferItem(file_name, size, curr_time(), friend.name, friend_number, file_number, is_incoming_transfer)
elem = QtGui.QListWidgetItem(self._messages) elem = QtGui.QListWidgetItem(self._messages)
elem.setSizeHint(QtCore.QSize(500, 100)) elem.setSizeHint(QtCore.QSize(600, 50))
self._messages.addItem(elem) self._messages.addItem(elem)
self._messages.setItemWidget(elem, item) self._messages.setItemWidget(elem, item)
self._messages.repaint() self._messages.repaint()
@ -720,20 +721,19 @@ class Profile(Contact, Singleton):
path = settings['auto_accept_path'] or curr_directory() path = settings['auto_accept_path'] or curr_directory()
self.accept_transfer(path + '/' + file_name, friend_number, file_number) self.accept_transfer(path + '/' + file_name, friend_number, file_number)
else: else:
self.create_file_transfer_item(file_name + ' ' + str(size), friend_number, file_number) self.create_file_transfer_item(file_name, size, friend_number, file_number, True)
#self._tox.file_control(friend_number, file_number, TOX_FILE_CONTROL['CANCEL'])
# TODO: show info about incoming transfer
def cancel_transfer(self, friend_number, file_number): def cancel_transfer(self, friend_number, file_number):
self._tox.file_control(friend_number, file_number, TOX_FILE_CONTROL['CANCEL'])
if (friend_number, file_number) in self._file_transfers: if (friend_number, file_number) in self._file_transfers:
tr = self._file_transfers[(friend_number, file_number)]
tr.cancel()
del self._file_transfers[(friend_number, file_number)] del self._file_transfers[(friend_number, file_number)]
def accept_transfer(self, path, friend_number, file_number): def accept_transfer(self, item, path, friend_number, file_number):
rt = ReceiveTransfer(path + '/' + path, self._tox, friend_number, file_number) rt = ReceiveTransfer(path, self._tox, friend_number, file_number)
self._file_transfers[(friend_number, file_number)] = rt self._file_transfers[(friend_number, file_number)] = rt
self._tox.file_control(friend_number, file_number, TOX_FILE_CONTROL['RESUME']) self._tox.file_control(friend_number, file_number, TOX_FILE_CONTROL['RESUME'])
# bind rt with widget rt.set_event_handler(item.update)
def incoming_avatar(self, friend_number, file_number, size): def incoming_avatar(self, friend_number, file_number, size):
""" """
@ -767,9 +767,8 @@ class Profile(Contact, Singleton):
friend_number = self.get_active_number() friend_number = self.get_active_number()
st = SendTransfer(path, self._tox, friend_number) st = SendTransfer(path, self._tox, friend_number)
self._file_transfers[(friend_number, st.get_file_number())] = st self._file_transfers[(friend_number, st.get_file_number())] = st
item = self.create_file_transfer_item('Out file', friend_number, st.get_file_number()) item = self.create_file_transfer_item('Out file', friend_number, st.get_file_number(), False)
st.set_event_handler(item.update) st.set_event_handler(item.update)
# bind st with widget
def outgoing_chunk(self, friend_number, file_number, position, size): def outgoing_chunk(self, friend_number, file_number, position, size):
transfer = self._file_transfers[(friend_number, file_number)] transfer = self._file_transfers[(friend_number, file_number)]