file control callback added, screenshots added

This commit is contained in:
ingvar1995 2016-03-23 17:27:05 +03:00
parent 779e81acb9
commit 2e333085f0
4 changed files with 51 additions and 8 deletions

View File

@ -177,8 +177,8 @@ 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
if file_control == TOX_FILE_CONTROL['CANCEL']:
Profile.get_instance().cancel_transfer(friend_number, file_number, True)
# -----------------------------------------------------------------------------------------------------------------
# Callbacks - initialization

View File

@ -50,6 +50,10 @@ class FileTransfer(QtCore.QObject):
self._file.close()
self._state_changed.signal.emit(self.state, self._done / self._size)
def cancelled(self):
self._file.close()
self._state_changed.signal.emit(TOX_FILE_CONTROL['CANCEL'], self._done / self._size)
def send_control(self, control):
if self._tox.file_control(self._friend_number, self._file_number, control):
self.state = control
@ -62,6 +66,10 @@ class FileTransfer(QtCore.QObject):
# TODO implement or not implement
pass
# -----------------------------------------------------------------------------------------------------------------
# Send file
# -----------------------------------------------------------------------------------------------------------------
class SendTransfer(FileTransfer):
@ -99,6 +107,28 @@ class SendAvatar(SendTransfer):
super(SendAvatar, self).__init__(path, tox, friend_number, TOX_FILE_KIND['AVATAR'], hash)
class SendFromBuffer(FileTransfer):
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)
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
# -----------------------------------------------------------------------------------------------------------------
class ReceiveTransfer(FileTransfer):
def __init__(self, path, tox, friend_number, size, file_number):

View File

@ -298,7 +298,6 @@ class MainWindow(QtGui.QMainWindow):
self.profile.send_file(name[0])
def send_screenshot(self):
# TODO: add screenshots support
if self.profile.is_active_online(): # active friend exists and online
self.sw = ScreenShotWindow()
self.sw.show()
@ -375,7 +374,7 @@ class MainWindow(QtGui.QMainWindow):
class ScreenShotWindow(QtGui.QWidget):
# TODO: send raw data and make window semi-transparent
# TODO: make window semi-transparent
def __init__(self):
super(ScreenShotWindow, self).__init__()
self.rubberband = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle, self)
@ -407,8 +406,11 @@ class ScreenShotWindow(QtGui.QWidget):
rect.y(),
rect.width(),
rect.height())
image = p.toImage()
print len(image.bits())
byte_array = QtCore.QByteArray()
buffer = QtCore.QBuffer(byte_array)
buffer.open(QtCore.QIODevice.WriteOnly)
p.save(buffer, 'PNG')
Profile.get_instance().send_screenshot(''.join(byte_array[i] for i in xrange(byte_array.length())))
self.close()

View File

@ -736,15 +736,19 @@ class Profile(Contact, Singleton):
friend = self.get_friend_by_number(friend_number)
if settings['allow_auto_accept'] and friend.tox_id in settings['auto_accept_from_friends']:
path = settings['auto_accept_path'] or curr_directory()
# TODO: check if file exists
item = self.create_file_transfer_item(file_name.decode('utf-8'), size, friend_number, file_number, False)
self.accept_transfer(item, path + '/' + file_name.decode('utf-8'), friend_number, file_number)
else:
self.create_file_transfer_item(file_name.decode('utf-8'), size, friend_number, file_number, True)
def cancel_transfer(self, friend_number, file_number):
def cancel_transfer(self, friend_number, file_number, already_cancelled=False):
if (friend_number, file_number) in self._file_transfers:
tr = self._file_transfers[(friend_number, file_number)]
tr.cancel()
if not already_cancelled:
tr.cancel()
else:
tr.cancelled()
del self._file_transfers[(friend_number, file_number)]
def accept_transfer(self, item, path, friend_number, file_number, size):
@ -783,6 +787,13 @@ class Profile(Contact, Singleton):
sa = SendAvatar(avatar_path, self._tox, friend_number)
self._file_transfers[(friend_number, sa.get_file_number())] = sa
def send_screenshot(self, data):
friend_number = self.get_active_number()
st = SendFromBuffer(self._tox, friend_number, data, 'toxygen_inline.png')
self._file_transfers[(friend_number, st.get_file_number())] = st
item = self.create_file_transfer_item('toxygen_inline.png', len(data), friend_number, st.get_file_number(), False)
st.set_state_changed_handler(item.update)
def send_file(self, path):
friend_number = self.get_active_number()
st = SendTransfer(path, self._tox, friend_number)