2016-03-18 14:20:07 +01:00
|
|
|
from toxcore_enums_and_consts import TOX_FILE_KIND, TOX_FILE_CONTROL
|
|
|
|
from os.path import basename, getsize, exists
|
2016-04-28 21:57:32 +02:00
|
|
|
from os import remove, rename
|
2016-04-20 11:32:28 +02:00
|
|
|
from time import time, sleep
|
2016-03-18 14:20:07 +01:00
|
|
|
from tox import Tox
|
2016-04-03 22:51:46 +02:00
|
|
|
import settings
|
2016-05-24 20:22:21 +02:00
|
|
|
try:
|
|
|
|
from PySide import QtCore
|
|
|
|
except ImportError:
|
|
|
|
from PyQt4 import QtCore
|
2016-03-16 21:11:18 +01:00
|
|
|
|
|
|
|
|
2016-03-17 18:46:18 +01:00
|
|
|
TOX_FILE_TRANSFER_STATE = {
|
|
|
|
'RUNNING': 0,
|
|
|
|
'PAUSED': 1,
|
|
|
|
'CANCELED': 2,
|
|
|
|
'FINISHED': 3,
|
2016-05-09 17:32:29 +02:00
|
|
|
'PAUSED_BY_FRIEND': 4
|
2016-03-17 18:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-21 18:19:13 +01:00
|
|
|
class StateSignal(QtCore.QObject):
|
2016-05-24 20:22:21 +02:00
|
|
|
try:
|
|
|
|
signal = QtCore.Signal(int, float) # state and progress
|
|
|
|
except:
|
|
|
|
signal = QtCore.pyqtSignal(int, float) # state and progress - pyqt4
|
2016-03-19 18:58:42 +01:00
|
|
|
|
|
|
|
|
2016-03-19 12:41:01 +01:00
|
|
|
class FileTransfer(QtCore.QObject):
|
2016-04-14 14:01:59 +02:00
|
|
|
"""
|
|
|
|
Superclass for file transfers
|
|
|
|
"""
|
2016-03-19 12:41:01 +01:00
|
|
|
|
2016-03-21 18:19:13 +01:00
|
|
|
def __init__(self, path, tox, friend_number, size, file_number=None):
|
2016-03-19 12:41:01 +01:00
|
|
|
QtCore.QObject.__init__(self)
|
2016-03-16 21:11:18 +01:00
|
|
|
self._path = path
|
|
|
|
self._tox = tox
|
|
|
|
self._friend_number = friend_number
|
2016-03-17 18:46:18 +01:00
|
|
|
self.state = TOX_FILE_TRANSFER_STATE['RUNNING']
|
|
|
|
self._file_number = file_number
|
2016-03-17 21:51:15 +01:00
|
|
|
self._creation_time = time()
|
2016-03-21 19:53:02 +01:00
|
|
|
self._size = float(size)
|
2016-03-21 18:19:13 +01:00
|
|
|
self._done = 0
|
|
|
|
self._state_changed = StateSignal()
|
2016-03-16 21:11:18 +01:00
|
|
|
|
|
|
|
def set_tox(self, tox):
|
|
|
|
self._tox = tox
|
|
|
|
|
2016-03-21 18:19:13 +01:00
|
|
|
def set_state_changed_handler(self, handler):
|
|
|
|
self._state_changed.signal.connect(handler)
|
2016-03-19 12:41:01 +01:00
|
|
|
|
2016-05-08 21:29:50 +02:00
|
|
|
def signal(self):
|
|
|
|
self._state_changed.signal.emit(self.state, self._done / self._size if self._size else 0)
|
|
|
|
|
2016-03-17 18:46:18 +01:00
|
|
|
def get_file_number(self):
|
|
|
|
return self._file_number
|
|
|
|
|
|
|
|
def get_friend_number(self):
|
|
|
|
return self._friend_number
|
|
|
|
|
2016-03-19 18:58:42 +01:00
|
|
|
def cancel(self):
|
|
|
|
self.send_control(TOX_FILE_CONTROL['CANCEL'])
|
2016-03-23 16:22:32 +01:00
|
|
|
if hasattr(self, '_file'):
|
|
|
|
self._file.close()
|
2016-04-14 14:01:59 +02:00
|
|
|
self._state_changed.signal.emit(self.state, 1)
|
2016-03-19 18:58:42 +01:00
|
|
|
|
2016-03-23 15:27:05 +01:00
|
|
|
def cancelled(self):
|
2016-03-23 16:22:32 +01:00
|
|
|
if hasattr(self, '_file'):
|
2016-04-20 11:32:28 +02:00
|
|
|
sleep(0.1)
|
2016-03-23 16:22:32 +01:00
|
|
|
self._file.close()
|
2016-05-08 21:29:50 +02:00
|
|
|
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'])
|
2016-05-09 17:32:29 +02:00
|
|
|
else:
|
|
|
|
self.state = TOX_FILE_TRANSFER_STATE['PAUSED_BY_FRIEND']
|
2016-05-08 21:29:50 +02:00
|
|
|
self.signal()
|
2016-03-23 15:27:05 +01:00
|
|
|
|
2016-03-17 18:46:18 +01:00
|
|
|
def send_control(self, control):
|
|
|
|
if self._tox.file_control(self._friend_number, self._file_number, control):
|
|
|
|
self.state = control
|
2016-05-08 21:29:50 +02:00
|
|
|
self.signal()
|
2016-03-17 18:46:18 +01:00
|
|
|
|
|
|
|
def get_file_id(self):
|
|
|
|
return self._tox.file_get_file_id(self._friend_number, self._file_number)
|
|
|
|
|
2016-03-23 15:27:05 +01:00
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
# Send file
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2016-03-16 21:11:18 +01:00
|
|
|
|
|
|
|
class SendTransfer(FileTransfer):
|
2016-03-19 18:58:42 +01:00
|
|
|
|
2016-03-18 14:20:07 +01:00
|
|
|
def __init__(self, path, tox, friend_number, kind=TOX_FILE_KIND['DATA'], file_id=None):
|
|
|
|
if path is not None:
|
|
|
|
self._file = open(path, 'rb')
|
2016-03-21 18:19:13 +01:00
|
|
|
size = getsize(path)
|
|
|
|
else:
|
|
|
|
size = 0
|
|
|
|
super(SendTransfer, self).__init__(path, tox, friend_number, size)
|
|
|
|
self._file_number = tox.file_send(friend_number, kind, size, file_id,
|
|
|
|
basename(path).encode('utf-8') if path else '')
|
2016-03-16 21:11:18 +01:00
|
|
|
|
|
|
|
def send_chunk(self, position, size):
|
2016-04-14 14:01:59 +02:00
|
|
|
"""
|
|
|
|
Send chunk
|
|
|
|
:param position: start position in file
|
|
|
|
:param size: chunk max size
|
|
|
|
"""
|
2016-03-18 14:50:32 +01:00
|
|
|
if size:
|
|
|
|
self._file.seek(position)
|
|
|
|
data = self._file.read(size)
|
2016-03-21 21:51:29 +01:00
|
|
|
self._tox.file_send_chunk(self._friend_number, self._file_number, position, data)
|
2016-03-21 18:19:13 +01:00
|
|
|
self._done += size
|
2016-05-08 21:29:50 +02:00
|
|
|
self.signal()
|
2016-03-18 14:50:32 +01:00
|
|
|
else:
|
2016-05-02 20:54:32 +02:00
|
|
|
if hasattr(self, '_file'):
|
|
|
|
self._file.close()
|
2016-03-18 14:50:32 +01:00
|
|
|
self.state = TOX_FILE_TRANSFER_STATE['FINISHED']
|
2016-03-21 19:53:02 +01:00
|
|
|
self._state_changed.signal.emit(self.state, 1)
|
2016-03-16 21:11:18 +01:00
|
|
|
|
|
|
|
|
2016-03-18 14:20:07 +01:00
|
|
|
class SendAvatar(SendTransfer):
|
2016-04-14 14:01:59 +02:00
|
|
|
"""
|
|
|
|
Send avatar to friend. Doesn't need file transfer item
|
|
|
|
"""
|
2016-03-19 18:58:42 +01:00
|
|
|
|
2016-03-18 14:20:07 +01:00
|
|
|
def __init__(self, path, tox, friend_number):
|
|
|
|
if path is None:
|
2016-03-21 18:19:13 +01:00
|
|
|
hash = None
|
2016-03-18 14:20:07 +01:00
|
|
|
else:
|
|
|
|
with open(path, 'rb') as fl:
|
|
|
|
hash = Tox.hash(fl.read())
|
2016-03-21 18:19:13 +01:00
|
|
|
super(SendAvatar, self).__init__(path, tox, friend_number, TOX_FILE_KIND['AVATAR'], hash)
|
2016-03-18 14:20:07 +01:00
|
|
|
|
|
|
|
|
2016-03-23 15:27:05 +01:00
|
|
|
class SendFromBuffer(FileTransfer):
|
2016-04-14 14:01:59 +02:00
|
|
|
"""
|
|
|
|
Send inline image
|
|
|
|
"""
|
2016-03-23 15:27:05 +01:00
|
|
|
|
|
|
|
def __init__(self, tox, friend_number, data, file_name):
|
|
|
|
super(SendFromBuffer, self).__init__(None, tox, friend_number, len(data))
|
|
|
|
self._data = data
|
|
|
|
self._file_number = tox.file_send(friend_number, TOX_FILE_KIND['DATA'], len(data), None, file_name)
|
|
|
|
|
2016-04-13 23:46:28 +02:00
|
|
|
def get_data(self):
|
|
|
|
return self._data
|
|
|
|
|
2016-03-23 15:27:05 +01:00
|
|
|
def send_chunk(self, position, size):
|
|
|
|
if size:
|
|
|
|
data = self._data[position:position + 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)
|
|
|
|
else:
|
|
|
|
self.state = TOX_FILE_TRANSFER_STATE['FINISHED']
|
|
|
|
self._state_changed.signal.emit(self.state, 1)
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
# Receive file
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2016-03-16 21:11:18 +01:00
|
|
|
class ReceiveTransfer(FileTransfer):
|
2016-03-19 18:58:42 +01:00
|
|
|
|
2016-03-21 18:19:13 +01:00
|
|
|
def __init__(self, path, tox, friend_number, size, file_number):
|
|
|
|
super(ReceiveTransfer, self).__init__(path, tox, friend_number, size, file_number)
|
2016-04-28 21:57:32 +02:00
|
|
|
self._file = open(self._path, 'wb')
|
|
|
|
self._file.truncate(0)
|
2016-03-21 18:19:13 +01:00
|
|
|
self._file_size = 0
|
2016-03-16 21:11:18 +01:00
|
|
|
|
2016-03-19 18:58:42 +01:00
|
|
|
def cancel(self):
|
|
|
|
super(ReceiveTransfer, self).cancel()
|
|
|
|
remove(self._path)
|
|
|
|
|
2016-03-16 21:11:18 +01:00
|
|
|
def write_chunk(self, position, data):
|
2016-04-14 14:01:59 +02:00
|
|
|
"""
|
|
|
|
Incoming chunk
|
|
|
|
:param position: position in file to save data
|
|
|
|
:param data: raw data (string)
|
|
|
|
"""
|
2016-03-21 18:19:13 +01:00
|
|
|
if data is None:
|
|
|
|
self._file.close()
|
|
|
|
self.state = TOX_FILE_TRANSFER_STATE['FINISHED']
|
2016-04-12 15:11:10 +02:00
|
|
|
self._state_changed.signal.emit(self.state, 1)
|
2016-03-21 18:19:13 +01:00
|
|
|
else:
|
2016-03-17 18:46:18 +01:00
|
|
|
data = ''.join(chr(x) for x in data)
|
2016-03-21 18:19:13 +01:00
|
|
|
if self._file_size < position:
|
2016-03-17 16:55:18 +01:00
|
|
|
self._file.seek(0, 2)
|
2016-03-21 18:19:13 +01:00
|
|
|
self._file.write('\0' * (position - self._file_size))
|
2016-03-16 22:07:09 +01:00
|
|
|
self._file.seek(position)
|
2016-03-17 18:46:18 +01:00
|
|
|
self._file.write(data)
|
2016-03-16 22:07:09 +01:00
|
|
|
self._file.flush()
|
2016-03-21 19:53:02 +01:00
|
|
|
l = len(data)
|
|
|
|
if position + l > self._file_size:
|
|
|
|
self._file_size = position + l
|
|
|
|
self._done += l
|
2016-06-04 14:19:15 +02:00
|
|
|
self.signal()
|
2016-03-18 14:20:07 +01:00
|
|
|
|
|
|
|
|
2016-04-12 15:11:10 +02:00
|
|
|
class ReceiveToBuffer(FileTransfer):
|
2016-04-14 14:01:59 +02:00
|
|
|
"""
|
|
|
|
Inline image - save in buffer not in file system
|
|
|
|
"""
|
2016-04-12 15:11:10 +02:00
|
|
|
|
|
|
|
def __init__(self, tox, friend_number, size, file_number):
|
|
|
|
super(ReceiveToBuffer, self).__init__(None, tox, friend_number, size, file_number)
|
|
|
|
self._data = ''
|
|
|
|
self._data_size = 0
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return self._data
|
|
|
|
|
|
|
|
def write_chunk(self, position, data):
|
|
|
|
if data is None:
|
|
|
|
self.state = TOX_FILE_TRANSFER_STATE['FINISHED']
|
|
|
|
self._state_changed.signal.emit(self.state, 1)
|
|
|
|
else:
|
|
|
|
data = ''.join(chr(x) for x in data)
|
|
|
|
l = len(data)
|
|
|
|
if self._data_size < position:
|
|
|
|
self._data += ('\0' * (position - self._data_size))
|
|
|
|
self._data = self._data[:position] + data + self._data[position + l:]
|
|
|
|
if position + l > self._data_size:
|
|
|
|
self._data_size = position + l
|
|
|
|
self._done += l
|
2016-05-08 21:29:50 +02:00
|
|
|
self.signal()
|
2016-04-12 15:11:10 +02:00
|
|
|
|
|
|
|
|
2016-03-18 14:20:07 +01:00
|
|
|
class ReceiveAvatar(ReceiveTransfer):
|
2016-04-14 14:01:59 +02:00
|
|
|
"""
|
|
|
|
Get friend's avatar. Doesn't need file transfer item
|
|
|
|
"""
|
2016-04-02 13:41:06 +02:00
|
|
|
MAX_AVATAR_SIZE = 512 * 1024
|
2016-03-19 18:58:42 +01:00
|
|
|
|
2016-03-21 18:19:13 +01:00
|
|
|
def __init__(self, tox, friend_number, size, file_number):
|
2016-04-28 21:08:47 +02:00
|
|
|
path = settings.ProfileHelper.get_path() + 'avatars/{}.png'.format(tox.friend_get_public_key(friend_number))
|
2016-04-28 21:57:32 +02:00
|
|
|
super(ReceiveAvatar, self).__init__(path + '.tmp', tox, friend_number, size, file_number)
|
2016-04-02 13:41:06 +02:00
|
|
|
if size > self.MAX_AVATAR_SIZE:
|
|
|
|
self.send_control(TOX_FILE_CONTROL['CANCEL'])
|
2016-04-28 21:57:32 +02:00
|
|
|
remove(path + '.tmp')
|
2016-05-25 15:05:16 +02:00
|
|
|
elif not size:
|
|
|
|
self.send_control(TOX_FILE_CONTROL['CANCEL'])
|
|
|
|
self._file.close()
|
|
|
|
if exists(path):
|
|
|
|
remove(path)
|
|
|
|
remove(path + '.tmp')
|
2016-04-02 13:41:06 +02:00
|
|
|
elif exists(path):
|
2016-05-25 15:05:16 +02:00
|
|
|
hash = self.get_file_id()
|
|
|
|
with open(path) as fl:
|
|
|
|
data = fl.read()
|
|
|
|
existing_hash = Tox.hash(data)
|
|
|
|
if hash == existing_hash:
|
2016-03-18 14:20:07 +01:00
|
|
|
self.send_control(TOX_FILE_CONTROL['CANCEL'])
|
2016-04-28 21:57:32 +02:00
|
|
|
remove(path + '.tmp')
|
2016-03-18 14:20:07 +01:00
|
|
|
else:
|
2016-05-25 15:05:16 +02:00
|
|
|
self.send_control(TOX_FILE_CONTROL['RESUME'])
|
2016-03-18 14:20:07 +01:00
|
|
|
else:
|
|
|
|
self.send_control(TOX_FILE_CONTROL['RESUME'])
|
2016-04-28 21:57:32 +02:00
|
|
|
|
|
|
|
def write_chunk(self, position, data):
|
|
|
|
super(ReceiveAvatar, self).write_chunk(position, data)
|
|
|
|
if self.state:
|
|
|
|
avatar_path = self._path[:-4]
|
|
|
|
if exists(avatar_path):
|
|
|
|
remove(avatar_path)
|
|
|
|
rename(self._path, avatar_path)
|
|
|
|
|
|
|
|
|