toxygen/src/profile.py

1191 lines
54 KiB
Python
Raw Normal View History

from list_items import *
2016-05-24 20:22:21 +02:00
try:
from PySide import QtCore, QtGui
except ImportError:
from PyQt4 import QtCore, QtGui
2016-06-08 21:53:41 +02:00
from friend import *
2016-04-03 22:51:46 +02:00
from settings import *
2016-02-20 19:21:56 +01:00
from toxcore_enums_and_consts import *
from ctypes import *
from util import log, Singleton, curr_directory
from tox_dns import tox_dns
2016-03-12 21:18:13 +01:00
from history import *
2016-03-16 21:56:35 +01:00
from file_transfers import *
2016-03-12 21:18:13 +01:00
import time
2016-04-24 12:45:11 +02:00
import calls
import avwidgets
2016-05-28 12:06:13 +02:00
import plugin_support
2016-02-18 17:15:38 +01:00
2016-06-08 21:53:41 +02:00
class Profile(contact.Contact, Singleton):
2016-02-26 15:32:36 +01:00
"""
2016-03-09 20:46:00 +01:00
Profile of current toxygen user. Contains friends list, tox instance
2016-02-26 15:32:36 +01:00
"""
2016-03-09 19:11:36 +01:00
def __init__(self, tox, screen):
"""
:param tox: tox instance
2016-03-09 19:11:36 +01:00
:param screen: ref to main screen
"""
2016-06-22 13:35:22 +02:00
contact.Contact.__init__(self,
tox.self_get_name(),
tox.self_get_status_message(),
screen.user_info,
tox.self_get_address())
Singleton.__init__(self)
2016-03-16 09:01:23 +01:00
self._screen = screen
2016-03-09 19:11:36 +01:00
self._messages = screen.messages
2016-03-16 09:01:23 +01:00
self._tox = tox
2016-03-18 14:50:32 +01:00
self._file_transfers = {} # dict of file transfers. key - tuple (friend_number, file_number)
2016-04-24 12:45:11 +02:00
self._call = calls.AV(tox.AV) # object with data about calls
2016-04-24 13:09:29 +02:00
self._incoming_calls = set()
settings = Settings.get_instance()
2016-03-16 09:01:23 +01:00
self._show_online = settings['show_online_friends']
2016-05-18 23:38:21 +02:00
screen.online_contacts.setCurrentIndex(int(self._show_online))
aliases = settings['friends_aliases']
2016-02-25 21:40:00 +01:00
data = tox.self_get_friend_list()
2016-03-15 21:54:01 +01:00
self._history = History(tox.self_get_public_key()) # connection to db
2016-03-09 20:46:00 +01:00
self._friends, self._active_friend = [], -1
for i in data: # creates list of friends
tox_id = tox.friend_get_public_key(i)
try:
2016-06-23 11:21:26 +02:00
alias = list(filter(lambda x: x[0] == tox_id, aliases))[0][1]
except:
alias = ''
2016-03-09 19:11:36 +01:00
item = self.create_friend_item()
name = alias or tox.friend_get_name(i) or tox_id
2016-02-25 21:40:00 +01:00
status_message = tox.friend_get_status_message(i)
2016-06-30 23:16:59 +02:00
if not self._history.friend_exists_in_db(tox_id):
self._history.add_friend_to_db(tox_id)
2016-03-15 21:54:01 +01:00
message_getter = self._history.messages_getter(tox_id)
2016-03-12 21:18:13 +01:00
friend = Friend(message_getter, i, name, status_message, item, tox_id)
friend.set_alias(alias)
self._friends.append(friend)
2016-03-16 09:01:23 +01:00
self.filtration(self._show_online)
2016-03-12 21:18:13 +01:00
2016-06-30 23:16:59 +02:00
# -----------------------------------------------------------------------------------------------------------------
# Edit current user's data
# -----------------------------------------------------------------------------------------------------------------
def change_status(self):
2016-03-09 20:46:00 +01:00
"""
Changes status of user (online, away, busy)
"""
if self._status is not None:
2016-05-24 20:08:52 +02:00
self.set_status((self._status + 1) % 3)
def set_status(self, status):
2016-06-25 14:40:01 +02:00
super(Profile, self).set_status(status)
if status is not None:
2016-05-30 20:38:21 +02:00
self._tox.self_set_status(status)
def set_name(self, value):
2016-06-23 21:50:17 +02:00
if self.name == value:
return
2016-06-24 14:00:13 +02:00
tmp = self.name
super(Profile, self).set_name(value.encode('utf-8'))
2016-03-16 09:01:23 +01:00
self._tox.self_set_name(self._name.encode('utf-8'))
2016-06-23 21:50:17 +02:00
message = QtGui.QApplication.translate("MainWindow", 'User {} is now known as {}', None,
QtGui.QApplication.UnicodeUTF8)
2016-06-24 14:00:13 +02:00
message = message.format(tmp, value)
2016-06-23 21:50:17 +02:00
for friend in self._friends:
friend.append_message(InfoMessage(message, time.time()))
if self._active_friend + 1:
self.create_message_item(message, time.time(), '', MESSAGE_TYPE['INFO_MESSAGE'])
2016-06-23 21:50:17 +02:00
self._messages.scrollToBottom()
def set_status_message(self, value):
2016-06-03 12:48:41 +02:00
super(Profile, self).set_status_message(value)
2016-03-16 09:01:23 +01:00
self._tox.self_set_status_message(self._status_message.encode('utf-8'))
2016-05-03 21:02:56 +02:00
def new_nospam(self):
2016-06-08 17:35:40 +02:00
"""Sets new nospam part of tox id"""
2016-05-03 21:02:56 +02:00
import random
self._tox.self_set_nospam(random.randint(0, 4294967295)) # no spam - uint32
self._tox_id = self._tox.self_get_address()
return self._tox_id
# -----------------------------------------------------------------------------------------------------------------
# Filtration
# -----------------------------------------------------------------------------------------------------------------
def filtration(self, show_online=True, filter_str=''):
2016-03-09 20:46:00 +01:00
"""
Filtration of friends list
:param show_online: show online only contacts
:param filter_str: show contacts which name contains this substring
"""
2016-03-02 21:55:12 +01:00
filter_str = filter_str.lower()
2016-03-09 19:45:38 +01:00
for index, friend in enumerate(self._friends):
2016-03-02 21:55:12 +01:00
friend.visibility = (friend.status is not None or not show_online) and (filter_str in friend.name.lower())
2016-06-17 21:35:05 +02:00
friend.visibility = friend.visibility or friend.messages or friend.actions
2016-03-09 19:45:38 +01:00
if friend.visibility:
2016-03-16 09:01:23 +01:00
self._screen.friends_list.item(index).setSizeHint(QtCore.QSize(250, 70))
2016-03-09 19:45:38 +01:00
else:
2016-03-16 09:01:23 +01:00
self._screen.friends_list.item(index).setSizeHint(QtCore.QSize(250, 0))
self._show_online, self._filter_string = show_online, filter_str
2016-03-09 19:11:36 +01:00
settings = Settings.get_instance()
2016-03-16 09:01:23 +01:00
settings['show_online_friends'] = self._show_online
2016-03-09 19:11:36 +01:00
settings.save()
def update_filtration(self):
2016-03-09 20:46:00 +01:00
"""
Update list of contacts when 1 of friends change connection status
"""
2016-03-16 09:01:23 +01:00
self.filtration(self._show_online, self._filter_string)
def get_friend_by_number(self, num):
2016-06-21 13:58:11 +02:00
return list(filter(lambda x: x.number == num, self._friends))[0]
2016-05-28 12:06:13 +02:00
def get_friend(self, num):
return self._friends[num]
# -----------------------------------------------------------------------------------------------------------------
# Work with active friend
# -----------------------------------------------------------------------------------------------------------------
def get_active(self):
2016-02-25 21:40:00 +01:00
return self._active_friend
2016-03-09 19:11:36 +01:00
def set_active(self, value=None):
"""
Change current active friend or update info
2016-03-09 19:11:36 +01:00
:param value: number of new active friend in friend's list or None to update active user's data
"""
2016-03-09 19:45:38 +01:00
if value is None and self._active_friend == -1: # nothing to update
return
if value == -1: # all friends were deleted
2016-03-16 09:01:23 +01:00
self._screen.account_name.setText('')
self._screen.account_status.setText('')
self._active_friend = -1
2016-03-16 09:01:23 +01:00
self._screen.account_avatar.setHidden(True)
2016-03-15 18:05:19 +01:00
self._messages.clear()
2016-03-16 09:01:23 +01:00
self._screen.messageEdit.clear()
return
try:
2016-04-27 20:10:53 +02:00
self.send_typing(False)
self._screen.typing.setVisible(False)
2016-03-09 19:11:36 +01:00
if value is not None:
2016-06-14 20:47:03 +02:00
if self._active_friend + 1:
2016-06-18 22:50:12 +02:00
try:
self._friends[self._active_friend].curr_text = self._screen.messageEdit.toPlainText()
except:
pass
2016-03-09 19:45:38 +01:00
self._active_friend = value
2016-03-13 13:06:06 +01:00
friend = self._friends[value]
2016-06-13 18:28:17 +02:00
self._friends[value].reset_messages()
2016-06-14 20:47:03 +02:00
self._screen.messageEdit.setPlainText(friend.curr_text)
2016-03-15 18:05:19 +01:00
self._messages.clear()
2016-03-13 13:06:06 +01:00
friend.load_corr()
messages = friend.get_corr()[-PAGE_SIZE:]
2016-03-13 13:06:06 +01:00
for message in messages:
2016-03-29 14:54:58 +02:00
if message.get_type() <= 1:
data = message.get_data()
self.create_message_item(data[0],
data[2],
2016-06-15 22:27:57 +02:00
data[1],
2016-03-29 14:54:58 +02:00
data[3])
2016-06-13 18:28:17 +02:00
elif message.get_type() == MESSAGE_TYPE['FILE_TRANSFER']:
if message.get_status() is None:
self.create_unsent_file_item(message)
continue
2016-03-29 14:54:58 +02:00
item = self.create_file_transfer_item(message)
if message.get_status() in ACTIVE_FILE_TRANSFERS: # active file transfer
try:
ft = self._file_transfers[(message.get_friend_number(), message.get_file_number())]
ft.set_state_changed_handler(item.update)
ft.signal()
except:
2016-06-21 13:58:11 +02:00
print('Incoming not started transfer - no info found')
2016-06-13 18:28:17 +02:00
elif message.get_type() == MESSAGE_TYPE['INLINE']: # inline
2016-04-12 15:11:10 +02:00
self.create_inline_item(message.get_data())
2016-06-13 18:28:17 +02:00
else: # info message
data = message.get_data()
self.create_message_item(data[0],
data[2],
2016-06-13 18:28:17 +02:00
'',
data[3])
2016-03-15 18:05:19 +01:00
self._messages.scrollToBottom()
2016-04-24 12:45:11 +02:00
if value in self._call:
self._screen.active_call()
2016-04-24 13:09:29 +02:00
elif value in self._incoming_calls:
self._screen.incoming_call()
2016-04-24 12:45:11 +02:00
else:
self._screen.call_finished()
2016-03-13 13:06:06 +01:00
else:
friend = self._friends[self._active_friend]
self._screen.account_name.setText(friend.name)
self._screen.account_status.setText(friend.status_message)
2016-04-27 18:13:04 +02:00
avatar_path = (ProfileHelper.get_path() + 'avatars/{}.png').format(friend.tox_id[:TOX_PUBLIC_KEY_SIZE * 2])
2016-03-09 19:11:36 +01:00
if not os.path.isfile(avatar_path): # load default image
avatar_path = curr_directory() + '/images/avatar.png'
2016-05-12 19:45:01 +02:00
os.chdir(os.path.dirname(avatar_path))
2016-03-09 19:11:36 +01:00
pixmap = QtGui.QPixmap(QtCore.QSize(64, 64))
2016-04-01 15:38:33 +02:00
pixmap.load(avatar_path)
self._screen.account_avatar.setScaledContents(False)
self._screen.account_avatar.setPixmap(pixmap.scaled(64, 64, QtCore.Qt.KeepAspectRatio))
2016-04-27 18:13:04 +02:00
self._screen.account_avatar.repaint() # comment?
2016-06-21 13:58:11 +02:00
except Exception as ex: # no friend found. ignore
2016-06-23 11:21:26 +02:00
log('Friend value: ' + str(value))
2016-06-21 13:58:11 +02:00
log('Error: ' + str(ex))
2016-03-24 22:15:07 +01:00
raise
2016-02-25 21:40:00 +01:00
active_friend = property(get_active, set_active)
2016-02-25 21:40:00 +01:00
def get_last_message(self):
2016-04-14 14:01:59 +02:00
return self._friends[self._active_friend].get_last_message_text()
def get_active_number(self):
2016-03-15 20:12:37 +01:00
return self._friends[self._active_friend].number if self._active_friend + 1 else -1
2016-02-25 21:40:00 +01:00
def get_active_name(self):
2016-03-15 20:42:24 +01:00
return self._friends[self._active_friend].name if self._active_friend + 1 else ''
2016-02-26 19:54:15 +01:00
def is_active_online(self):
return self._active_friend + 1 and self._friends[self._active_friend].status is not None
2016-06-13 18:28:17 +02:00
def new_name(self, number, name):
friend = self.get_friend_by_number(number)
tmp = friend.name
friend.set_name(name)
name = str(name, 'utf-8')
2016-06-13 18:28:17 +02:00
if friend.name == name and tmp != name:
message = QtGui.QApplication.translate("MainWindow", 'User {} is now known as {}', None, QtGui.QApplication.UnicodeUTF8)
message = message.format(tmp, name)
friend.append_message(InfoMessage(message, time.time()))
2016-06-16 14:48:29 +02:00
friend.actions = True
2016-06-17 21:35:05 +02:00
if number == self.get_active_number():
self.create_message_item(message, time.time(), '', MESSAGE_TYPE['INFO_MESSAGE'])
2016-06-13 18:28:17 +02:00
self._messages.scrollToBottom()
self.set_active(None)
2016-05-02 17:27:46 +02:00
def update(self):
if self._active_friend + 1:
self.set_active(self._active_friend)
2016-06-07 11:27:58 +02:00
# -----------------------------------------------------------------------------------------------------------------
# Friend connection status callbacks
# -----------------------------------------------------------------------------------------------------------------
def send_files(self, friend_number):
friend = self.get_friend_by_number(friend_number)
files = friend.get_unsent_files()
try:
for fl in files:
data = fl.get_data()
if data[1] is not None:
self.send_inline(data[1], data[0], friend_number, True)
else:
self.send_file(data[0], friend_number, True)
friend.clear_unsent_files()
if friend_number == self.get_active_number():
self.update()
2016-06-17 23:43:30 +02:00
except Exception as ex:
2016-06-21 13:58:11 +02:00
print('Exception in file sending: ' + str(ex))
2016-06-04 14:19:15 +02:00
def friend_exit(self, friend_number):
2016-05-30 21:26:07 +02:00
"""
Friend with specified number quit
"""
2016-06-06 12:05:11 +02:00
# TODO: fix and add full file resuming support
self.get_friend_by_number(friend_number).status = None
self.friend_typing(friend_number, False)
if friend_number in self._call:
self._call.finish_call(friend_number, True)
2016-04-27 20:10:53 +02:00
# -----------------------------------------------------------------------------------------------------------------
# Typing notifications
# -----------------------------------------------------------------------------------------------------------------
def send_typing(self, typing):
"""
Send typing notification to a friend
"""
2016-06-06 20:18:32 +02:00
if Settings.get_instance()['typing_notifications'] and self._active_friend + 1:
2016-04-27 20:10:53 +02:00
friend = self._friends[self._active_friend]
if friend.status is not None:
self._tox.self_set_typing(friend.number, typing)
def friend_typing(self, friend_number, typing):
"""
Display incoming typing notification
"""
2016-04-27 20:10:53 +02:00
if friend_number == self.get_active_number():
self._screen.typing.setVisible(typing)
# -----------------------------------------------------------------------------------------------------------------
# Private messages
# -----------------------------------------------------------------------------------------------------------------
2016-06-15 23:12:27 +02:00
def receipt(self):
i = 0
while i < self._messages.count() and not self._messages.itemWidget(self._messages.item(i)).mark_as_sent():
i += 1
2016-06-04 21:17:32 +02:00
def send_messages(self, friend_number):
"""
Send 'offline' messages to friend
"""
friend = self.get_friend_by_number(friend_number)
friend.load_corr()
messages = friend.get_unsent_messages()
2016-06-04 21:17:32 +02:00
try:
for message in messages:
self.split_and_send(friend_number, message.get_data()[-1], message.get_data()[0].encode('utf-8'))
2016-06-23 11:21:26 +02:00
friend.inc_receipts()
2016-06-04 21:17:32 +02:00
except:
pass
2016-03-16 16:15:55 +01:00
def split_and_send(self, number, message_type, message):
"""
Message splitting
:param number: friend's number
:param message_type: type of message
:param message: message text
"""
while len(message) > TOX_MAX_MESSAGE_LENGTH:
size = TOX_MAX_MESSAGE_LENGTH * 4 / 5
2016-03-16 17:06:15 +01:00
last_part = message[size:TOX_MAX_MESSAGE_LENGTH]
2016-03-16 16:15:55 +01:00
if ' ' in last_part:
index = last_part.index(' ')
elif ',' in last_part:
index = last_part.index(',')
elif '.' in last_part:
index = last_part.index('.')
else:
index = TOX_MAX_MESSAGE_LENGTH - size - 1
2016-03-16 17:06:15 +01:00
index += size + 1
2016-03-16 16:15:55 +01:00
self._tox.friend_send_message(number, message_type, message[:index])
message = message[index:]
self._tox.friend_send_message(number, message_type, message)
2016-03-09 19:11:36 +01:00
def new_message(self, friend_num, message_type, message):
"""
Current user gets new message
2016-03-09 19:11:36 +01:00
:param friend_num: friend_num of friend who sent message
2016-03-09 19:45:38 +01:00
:param message_type: message type - plain text or action message (/me)
:param message: text of message
"""
2016-03-09 19:45:38 +01:00
if friend_num == self.get_active_number(): # add message to list
t = time.time()
self.create_message_item(message, t, MESSAGE_OWNER['FRIEND'], message_type)
2016-03-14 20:30:51 +01:00
self._messages.scrollToBottom()
2016-03-29 14:54:58 +02:00
self._friends[self._active_friend].append_message(
TextMessage(message, MESSAGE_OWNER['FRIEND'], t, message_type))
2016-02-26 19:54:15 +01:00
else:
2016-03-28 23:07:42 +02:00
friend = self.get_friend_by_number(friend_num)
2016-06-13 18:28:17 +02:00
friend.inc_messages()
2016-03-29 14:54:58 +02:00
friend.append_message(
2016-06-21 13:58:11 +02:00
TextMessage(message, MESSAGE_OWNER['FRIEND'], time.time(), message_type))
2016-05-30 20:38:21 +02:00
if not friend.visibility:
self.update_filtration()
2016-02-26 19:54:15 +01:00
def send_message(self, text):
"""
Send message to active friend
:param text: message text
"""
2016-05-28 12:06:13 +02:00
if text.startswith('/plugin '):
plugin_support.PluginLoader.get_instance().command(text[8:])
self._screen.messageEdit.clear()
2016-06-11 12:36:52 +02:00
elif text and self._active_friend + 1:
2016-03-04 18:52:52 +01:00
if text.startswith('/me '):
message_type = TOX_MESSAGE_TYPE['ACTION']
2016-03-04 18:52:52 +01:00
text = text[4:]
else:
message_type = TOX_MESSAGE_TYPE['NORMAL']
2016-03-12 21:18:13 +01:00
friend = self._friends[self._active_friend]
2016-06-15 22:27:57 +02:00
friend.inc_receipts()
2016-06-04 21:17:32 +02:00
if friend.status is not None:
self.split_and_send(friend.number, message_type, text.encode('utf-8'))
t = time.time()
self.create_message_item(text, t, MESSAGE_OWNER['NOT_SENT'], message_type)
2016-03-16 09:01:23 +01:00
self._screen.messageEdit.clear()
2016-03-14 20:30:51 +01:00
self._messages.scrollToBottom()
friend.append_message(TextMessage(text, MESSAGE_OWNER['NOT_SENT'], t, message_type))
def delete_message(self, time):
friend = self._friends[self._active_friend]
friend.delete_message(time)
self._history.delete_message(friend.tox_id, time)
self.update()
2016-02-27 18:03:33 +01:00
2016-03-13 13:06:06 +01:00
# -----------------------------------------------------------------------------------------------------------------
# History support
# -----------------------------------------------------------------------------------------------------------------
def save_history(self):
"""
Save history to db
"""
s = Settings.get_instance()
2016-03-18 14:50:32 +01:00
if hasattr(self, '_history'):
if s['save_history']:
2016-03-18 14:50:32 +01:00
for friend in self._friends:
2016-06-28 12:51:11 +02:00
if not self._history.friend_exists_in_db(friend.tox_id):
self._history.add_friend_to_db(friend.tox_id)
if not s['save_unsent_only']:
messages = friend.get_corr_for_saving()
else:
messages = friend.get_unsent_messages_for_saving()
2016-06-28 12:51:11 +02:00
self._history.delete_messages(friend.tox_id)
2016-03-18 14:50:32 +01:00
self._history.save_messages_to_db(friend.tox_id, messages)
unsent_messages = friend.get_unsent_messages()
2016-06-13 23:32:45 +02:00
unsent_time = unsent_messages[0].get_data()[2] if len(unsent_messages) else time.time() + 1
self._history.update_messages(friend.tox_id, unsent_time)
2016-05-15 22:02:05 +02:00
self._history.save()
2016-03-18 14:50:32 +01:00
del self._history
2016-03-13 13:06:06 +01:00
def clear_history(self, num=None, save_unsent=False):
2016-05-30 21:26:07 +02:00
"""
Clear chat history
"""
2016-03-13 13:06:06 +01:00
if num is not None:
friend = self._friends[num]
friend.clear_corr(save_unsent)
2016-03-24 04:49:04 +01:00
if self._history.friend_exists_in_db(friend.tox_id):
self._history.delete_messages(friend.tox_id)
self._history.delete_friend_from_db(friend.tox_id)
2016-03-13 13:06:06 +01:00
else: # clear all history
2016-06-21 13:58:11 +02:00
for number in range(len(self._friends)):
self.clear_history(number, save_unsent)
2016-03-13 13:06:06 +01:00
if num is None or num == self.get_active_number():
self.update()
2016-03-13 13:06:06 +01:00
2016-03-24 22:15:07 +01:00
def load_history(self):
"""
Tries to load next part of messages
"""
friend = self._friends[self._active_friend]
friend.load_corr(False)
data = friend.get_corr()
2016-03-24 22:15:07 +01:00
if not data:
return
data.reverse()
data = data[self._messages.count():self._messages.count() + PAGE_SIZE]
2016-03-24 22:15:07 +01:00
for message in data:
2016-06-08 17:35:40 +02:00
if message.get_type() <= 1: # text message
2016-03-29 16:11:30 +02:00
data = message.get_data()
self.create_message_item(data[0],
data[2],
2016-06-15 22:27:57 +02:00
data[1],
2016-03-29 16:11:30 +02:00
data[3],
False)
2016-06-08 17:35:40 +02:00
elif message.get_type() == MESSAGE_TYPE['FILE_TRANSFER']:
2016-06-15 20:02:59 +02:00
if message.get_status() is None:
self.create_unsent_file_item(message)
continue
2016-03-29 16:11:30 +02:00
item = self.create_file_transfer_item(message, False)
if message.get_status() in ACTIVE_FILE_TRANSFERS: # active file transfer
2016-06-17 14:19:38 +02:00
try:
ft = self._file_transfers[(message.get_friend_number(), message.get_file_number())]
ft.set_state_changed_handler(item.update)
ft.signal()
except:
2016-06-21 13:58:11 +02:00
print('Incoming not started transfer - no info found')
2016-06-13 18:28:17 +02:00
elif message.get_type() == MESSAGE_TYPE['INLINE']: # inline
self.create_inline_item(message.get_data())
else: # info message
data = message.get_data()
self.create_message_item(data[0],
data[2],
2016-06-13 18:28:17 +02:00
'',
data[3])
2016-03-24 22:15:07 +01:00
2016-03-15 21:35:15 +01:00
def export_history(self, directory):
2016-03-15 21:54:01 +01:00
self._history.export(directory)
2016-03-15 21:35:15 +01:00
# -----------------------------------------------------------------------------------------------------------------
2016-03-18 22:28:53 +01:00
# Factories for friend, message and file transfer items
2016-03-12 21:18:13 +01:00
# -----------------------------------------------------------------------------------------------------------------
def create_friend_item(self):
"""
Method-factory
:return: new widget for friend instance
"""
item = ContactItem()
2016-03-16 09:01:23 +01:00
elem = QtGui.QListWidgetItem(self._screen.friends_list)
2016-03-12 21:18:13 +01:00
elem.setSizeHint(QtCore.QSize(250, 70))
2016-03-16 09:01:23 +01:00
self._screen.friends_list.addItem(elem)
self._screen.friends_list.setItemWidget(elem, item)
2016-03-12 21:18:13 +01:00
return item
2016-06-15 22:27:57 +02:00
def create_message_item(self, text, time, owner, message_type, append=True):
2016-06-17 23:43:30 +02:00
if message_type == MESSAGE_TYPE['INFO_MESSAGE']:
name = ''
elif owner == MESSAGE_OWNER['FRIEND']:
name = self.get_active_name()
else:
name = self._name
2016-06-15 22:27:57 +02:00
item = MessageItem(text, time, name, owner != MESSAGE_OWNER['NOT_SENT'], message_type, self._messages)
2016-03-24 22:30:26 +01:00
elem = QtGui.QListWidgetItem()
elem.setSizeHint(QtCore.QSize(self._messages.width(), item.height()))
2016-03-24 22:15:07 +01:00
if append:
self._messages.addItem(elem)
else:
2016-03-24 22:30:26 +01:00
self._messages.insertItem(0, elem)
self._messages.setItemWidget(elem, item)
2016-03-13 13:06:06 +01:00
2016-03-29 14:54:58 +02:00
def create_file_transfer_item(self, tm, append=True):
data = list(tm.get_data())
data[3] = self.get_friend_by_number(data[4]).name if data[3] else self._name
2016-05-02 17:27:46 +02:00
data.append(self._messages.width())
2016-03-29 14:54:58 +02:00
item = FileTransferItem(*data)
elem = QtGui.QListWidgetItem()
2016-05-09 21:37:11 +02:00
elem.setSizeHint(QtCore.QSize(self._messages.width() - 30, 34))
2016-03-29 14:54:58 +02:00
if append:
self._messages.addItem(elem)
else:
self._messages.insertItem(0, elem)
2016-03-18 22:28:53 +01:00
self._messages.setItemWidget(elem, item)
2016-03-19 12:41:01 +01:00
return item
2016-03-18 22:28:53 +01:00
def create_unsent_file_item(self, message, append=True):
data = message.get_data()
item = UnsentFileItem(os.path.basename(data[0]),
os.path.getsize(data[0]) if data[1] is None else len(data[1]),
self.name,
data[2],
self._messages.width())
elem = QtGui.QListWidgetItem()
elem.setSizeHint(QtCore.QSize(self._messages.width() - 30, 34))
if append:
self._messages.addItem(elem)
else:
self._messages.insertItem(0, elem)
self._messages.setItemWidget(elem, item)
2016-04-12 15:11:10 +02:00
def create_inline_item(self, data, append=True):
elem = QtGui.QListWidgetItem()
item = InlineImageItem(data, self._messages.width(), elem)
2016-05-02 17:27:46 +02:00
elem.setSizeHint(QtCore.QSize(self._messages.width(), item.height()))
2016-04-12 15:11:10 +02:00
if append:
self._messages.addItem(elem)
else:
self._messages.insertItem(0, elem)
self._messages.setItemWidget(elem, item)
2016-03-12 21:18:13 +01:00
# -----------------------------------------------------------------------------------------------------------------
# Work with friends (remove, block, set alias, get public key)
# -----------------------------------------------------------------------------------------------------------------
2016-03-11 12:37:45 +01:00
def set_alias(self, num):
2016-05-30 21:26:07 +02:00
"""
Set new alias for friend
"""
friend = self._friends[num]
2016-06-23 11:21:26 +02:00
name = friend.name
2016-04-14 19:45:29 +02:00
dialog = QtGui.QApplication.translate('MainWindow',
"Enter new alias for friend {} or leave empty to use friend's name:",
None, QtGui.QApplication.UnicodeUTF8)
2016-06-21 13:58:11 +02:00
dialog = dialog.format(name)
2016-04-14 19:45:29 +02:00
title = QtGui.QApplication.translate('MainWindow',
'Set alias',
None, QtGui.QApplication.UnicodeUTF8)
text, ok = QtGui.QInputDialog.getText(None,
title,
2016-06-04 14:19:15 +02:00
dialog,
QtGui.QLineEdit.Normal,
2016-06-21 13:58:11 +02:00
name)
if ok:
settings = Settings.get_instance()
aliases = settings['friends_aliases']
if text:
2016-06-23 11:21:26 +02:00
friend.name = bytes(text, 'utf-8')
try:
2016-06-23 11:21:26 +02:00
index = list(map(lambda x: x[0], aliases)).index(friend.tox_id)
aliases[index] = (friend.tox_id, text)
except:
aliases.append((friend.tox_id, text))
friend.set_alias(text)
else: # use default name
2016-06-23 11:21:26 +02:00
friend.name = bytes(self._tox.friend_get_name(friend.number), 'utf-8')
friend.set_alias('')
try:
2016-06-23 11:21:26 +02:00
index = list(map(lambda x: x[0], aliases)).index(friend.tox_id)
del aliases[index]
except:
pass
settings.save()
2016-06-23 11:21:26 +02:00
if num == self.get_active_number():
self.update()
def friend_public_key(self, num):
return self._friends[num].tox_id
2016-03-11 12:37:45 +01:00
def delete_friend(self, num):
"""
Removes friend from contact list
:param num: number of friend in list
"""
friend = self._friends[num]
2016-06-08 17:35:40 +02:00
settings = Settings.get_instance()
2016-05-06 15:13:33 +02:00
try:
2016-06-23 11:21:26 +02:00
index = list(map(lambda x: x[0], settings['friends_aliases'])).index(friend.tox_id)
2016-05-06 15:13:33 +02:00
del settings['friends_aliases'][index]
except:
pass
2016-06-08 17:35:40 +02:00
if friend.tox_id in settings['notes']:
del settings['notes'][friend.tox_id]
settings.save()
2016-03-12 21:18:13 +01:00
self.clear_history(num)
2016-03-24 04:49:04 +01:00
if self._history.friend_exists_in_db(friend.tox_id):
self._history.delete_friend_from_db(friend.tox_id)
2016-03-16 09:01:23 +01:00
self._tox.friend_delete(friend.number)
2016-03-11 12:37:45 +01:00
del self._friends[num]
2016-03-16 09:01:23 +01:00
self._screen.friends_list.takeItem(num)
2016-03-11 12:37:45 +01:00
if num == self._active_friend: # active friend was deleted
if not len(self._friends): # last friend was deleted
self.set_active(-1)
else:
self.set_active(0)
2016-05-04 18:38:24 +02:00
data = self._tox.get_savedata()
2016-05-14 12:18:17 +02:00
ProfileHelper.get_instance().save_profile(data)
2016-03-11 12:37:45 +01:00
def add_friend(self, tox_id):
2016-05-30 21:26:07 +02:00
"""
Adds friend to list
"""
num = self._tox.friend_add_norequest(tox_id) # num - friend number
item = self.create_friend_item()
try:
if not self._history.friend_exists_in_db(tox_id):
self._history.add_friend_to_db(tox_id)
message_getter = self._history.messages_getter(tox_id)
except Exception as ex: # something is wrong
log('Accept friend request failed! ' + str(ex))
message_getter = None
friend = Friend(message_getter, num, tox_id, '', item, tox_id)
self._friends.append(friend)
def block_user(self, tox_id):
"""
Block user with specified tox id (or public key) - delete from friends list and ignore friend requests
"""
tox_id = tox_id[:TOX_PUBLIC_KEY_SIZE * 2]
if tox_id == self.tox_id[:TOX_PUBLIC_KEY_SIZE * 2]:
return
settings = Settings.get_instance()
if tox_id not in settings['blocked']:
settings['blocked'].append(tox_id)
settings.save()
try:
num = self._tox.friend_by_public_key(tox_id)
self.delete_friend(num)
2016-05-04 18:38:24 +02:00
data = self._tox.get_savedata()
2016-05-14 12:18:17 +02:00
ProfileHelper.get_instance().save_profile(data)
except: # not in friend list
pass
def unblock_user(self, tox_id, add_to_friend_list):
"""
Unblock user
:param tox_id: tox id of contact
:param add_to_friend_list: add this contact to friend list or not
"""
s = Settings.get_instance()
s['blocked'].remove(tox_id)
s.save()
if add_to_friend_list:
self.add_friend(tox_id)
2016-05-04 18:38:24 +02:00
data = self._tox.get_savedata()
2016-05-14 12:18:17 +02:00
ProfileHelper.get_instance().save_profile(data)
2016-03-11 12:37:45 +01:00
# -----------------------------------------------------------------------------------------------------------------
# Friend requests
# -----------------------------------------------------------------------------------------------------------------
2016-03-09 20:46:00 +01:00
def send_friend_request(self, tox_id, message):
2016-03-09 19:11:36 +01:00
"""
2016-03-09 20:46:00 +01:00
Function tries to send request to contact with specified id
:param tox_id: id of new contact or tox dns 4 value
2016-03-09 19:11:36 +01:00
:param message: additional message
:return: True on success else error string
"""
try:
message = message or 'Hello! Add me to your contact list please'
if '@' in tox_id: # value like groupbot@toxme.io
2016-03-12 16:34:10 +01:00
tox_id = tox_dns(tox_id)
if tox_id is None:
raise Exception('TOX DNS lookup failed')
if len(tox_id) == TOX_PUBLIC_KEY_SIZE * 2: # public key
self.add_friend(tox_id)
2016-05-09 22:09:07 +02:00
msgBox = QtGui.QMessageBox()
msgBox.setWindowTitle(QtGui.QApplication.translate("MainWindow", "Friend added", None, QtGui.QApplication.UnicodeUTF8))
text = (QtGui.QApplication.translate("MainWindow", 'Friend added without sending friend request', None, QtGui.QApplication.UnicodeUTF8))
msgBox.setText(text)
msgBox.exec_()
else:
result = self._tox.friend_add(tox_id, message.encode('utf-8'))
tox_id = tox_id[:TOX_PUBLIC_KEY_SIZE * 2]
item = self.create_friend_item()
if not self._history.friend_exists_in_db(tox_id):
self._history.add_friend_to_db(tox_id)
message_getter = self._history.messages_getter(tox_id)
friend = Friend(message_getter, result, tox_id, '', item, tox_id)
self._friends.append(friend)
2016-05-04 18:38:24 +02:00
data = self._tox.get_savedata()
2016-05-14 12:18:17 +02:00
ProfileHelper.get_instance().save_profile(data)
2016-03-09 19:11:36 +01:00
return True
except Exception as ex: # wrong data
log('Friend request failed with ' + str(ex))
2016-03-09 19:11:36 +01:00
return str(ex)
def process_friend_request(self, tox_id, message):
2016-03-09 20:46:00 +01:00
"""
Accept or ignore friend request
:param tox_id: tox id of contact
:param message: message
"""
2016-03-09 19:11:36 +01:00
try:
2016-04-04 11:20:32 +02:00
text = QtGui.QApplication.translate('MainWindow', 'User {} wants to add you to contact list. Message:\n{}', None, QtGui.QApplication.UnicodeUTF8)
info = text.format(tox_id, message)
fr_req = QtGui.QApplication.translate('MainWindow', 'Friend request', None, QtGui.QApplication.UnicodeUTF8)
reply = QtGui.QMessageBox.question(None, fr_req, info, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
2016-03-09 19:11:36 +01:00
if reply == QtGui.QMessageBox.Yes: # accepted
self.add_friend(tox_id)
2016-05-04 18:38:24 +02:00
data = self._tox.get_savedata()
2016-05-14 12:18:17 +02:00
ProfileHelper.get_instance().save_profile(data)
2016-03-09 20:46:00 +01:00
except Exception as ex: # something is wrong
2016-03-09 19:11:36 +01:00
log('Accept friend request failed! ' + str(ex))
2016-03-14 20:30:51 +01:00
# -----------------------------------------------------------------------------------------------------------------
# Reset
# -----------------------------------------------------------------------------------------------------------------
2016-03-15 18:05:19 +01:00
def reset(self, restart):
2016-03-14 20:30:51 +01:00
"""
Recreate tox instance
2016-03-15 18:05:19 +01:00
:param restart: method which calls restart and returns new tox instance
2016-03-14 20:30:51 +01:00
"""
2016-03-25 14:45:27 +01:00
for key in self._file_transfers.keys():
self._file_transfers[key].cancel()
del self._file_transfers[key]
2016-05-04 18:38:24 +02:00
self._call.stop()
2016-03-16 09:01:23 +01:00
del self._tox
self._tox = restart()
2016-03-15 18:05:19 +01:00
self.status = None
for friend in self._friends:
friend.status = None
2016-03-14 20:30:51 +01:00
2016-04-24 12:45:11 +02:00
def close(self):
2016-05-30 20:38:21 +02:00
if hasattr(self, '_call'):
2016-05-28 21:43:51 +02:00
self._call.stop()
del self._call
2016-06-28 12:51:11 +02:00
for i in range(len(self._friends)):
del self._friends[0]
2016-04-24 12:45:11 +02:00
# -----------------------------------------------------------------------------------------------------------------
# File transfers support
# -----------------------------------------------------------------------------------------------------------------
def incoming_file_transfer(self, friend_number, file_number, size, file_name):
2016-03-24 12:01:07 +01:00
"""
New transfer
:param friend_number: number of friend who sent file
:param file_number: file number
:param size: file size in bytes
:param file_name: file name without path
"""
2016-03-18 17:33:54 +01:00
settings = Settings.get_instance()
friend = self.get_friend_by_number(friend_number)
2016-04-12 15:11:10 +02:00
auto = settings['allow_auto_accept'] and friend.tox_id in settings['auto_accept_from_friends']
inline = (file_name in ALLOWED_FILES) and settings['allow_inline']
2016-04-12 15:11:10 +02:00
if inline and size < 1024 * 1024:
self.accept_transfer(None, '', friend_number, file_number, size, True)
tm = TransferMessage(MESSAGE_OWNER['FRIEND'],
time.time(),
TOX_FILE_TRANSFER_STATE['RUNNING'],
2016-04-12 15:11:10 +02:00
size,
file_name,
friend_number,
file_number)
2016-04-12 15:11:10 +02:00
elif auto:
2016-03-18 17:33:54 +01:00
path = settings['auto_accept_path'] or curr_directory()
2016-04-28 10:30:58 +02:00
if not os.path.isdir(path):
path = curr_directory()
2016-03-25 14:45:27 +01:00
new_file_name, i = file_name, 1
while os.path.isfile(path + '/' + new_file_name): # file with same name already exists
if '.' in file_name: # has extension
d = file_name.rindex('.')
else: # no extension
d = len(file_name)
2016-03-29 14:54:58 +02:00
new_file_name = file_name[:d] + ' ({})'.format(i) + file_name[d:]
2016-03-25 14:45:27 +01:00
i += 1
self.accept_transfer(None, path + '/' + new_file_name, friend_number, file_number, size)
2016-03-29 14:54:58 +02:00
tm = TransferMessage(MESSAGE_OWNER['FRIEND'],
time.time(),
TOX_FILE_TRANSFER_STATE['RUNNING'],
2016-03-29 14:54:58 +02:00
size,
new_file_name,
friend_number,
file_number)
2016-03-18 17:33:54 +01:00
else:
2016-03-29 14:54:58 +02:00
tm = TransferMessage(MESSAGE_OWNER['FRIEND'],
time.time(),
TOX_FILE_TRANSFER_STATE['INCOMING_NOT_STARTED'],
2016-03-29 14:54:58 +02:00
size,
file_name,
friend_number,
file_number)
if friend_number == self.get_active_number():
item = self.create_file_transfer_item(tm)
if (inline and size < 1024 * 1024) or auto:
self._file_transfers[(friend_number, file_number)].set_state_changed_handler(item.update)
2016-03-29 16:11:30 +02:00
self._messages.scrollToBottom()
2016-03-29 14:54:58 +02:00
else:
2016-06-16 14:48:29 +02:00
friend.actions = True
2016-03-29 14:54:58 +02:00
friend.append_message(tm)
def cancel_transfer(self, friend_number, file_number, already_cancelled=False):
2016-03-25 14:45:27 +01:00
"""
Stop transfer
:param friend_number: number of friend
:param file_number: file number
:param already_cancelled: was cancelled by friend
"""
i = self.get_friend_by_number(friend_number).update_transfer_data(file_number,
TOX_FILE_TRANSFER_STATE['CANCELLED'])
2016-03-19 12:41:01 +01:00
if (friend_number, file_number) in self._file_transfers:
tr = self._file_transfers[(friend_number, file_number)]
if not already_cancelled:
tr.cancel()
else:
tr.cancelled()
if (friend_number, file_number) in self._file_transfers:
del tr
del self._file_transfers[(friend_number, file_number)]
2016-03-29 14:54:58 +02:00
else:
if not already_cancelled:
self._tox.file_control(friend_number, file_number, TOX_FILE_CONTROL['CANCEL'])
if friend_number == self.get_active_number():
tmp = self._messages.count() + i
if tmp >= 0:
2016-06-17 23:43:30 +02:00
self._messages.itemWidget(self._messages.item(tmp)).update(TOX_FILE_TRANSFER_STATE['CANCELLED'],
0, -1)
2016-03-19 12:41:01 +01:00
def cancel_not_started_transfer(self, time):
self._friends[self._active_friend].delete_one_unsent_file(time)
self.update()
def pause_transfer(self, friend_number, file_number, by_friend=False):
"""
Pause transfer with specified data
"""
tr = self._file_transfers[(friend_number, file_number)]
tr.pause(by_friend)
t = TOX_FILE_TRANSFER_STATE['PAUSED_BY_FRIEND'] if by_friend else TOX_FILE_TRANSFER_STATE['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):
"""
Resume transfer with specified data
"""
self.get_friend_by_number(friend_number).update_transfer_data(file_number,
TOX_FILE_TRANSFER_STATE['RUNNING'])
# if (friend_number, file_number) not in self._file_transfers:
# print self._file_transfers
# print (friend_number, file_number)
# return
2016-05-09 17:32:29 +02:00
tr = self._file_transfers[(friend_number, file_number)]
if by_friend:
tr.state = TOX_FILE_TRANSFER_STATE['RUNNING']
tr.signal()
2016-05-11 11:01:29 +02:00
else: # send seek control?
2016-05-09 17:32:29 +02:00
tr.send_control(TOX_FILE_CONTROL['RESUME'])
2016-04-12 15:11:10 +02:00
def accept_transfer(self, item, path, friend_number, file_number, size, inline=False):
2016-03-25 14:45:27 +01:00
"""
:param item: transfer item.
2016-03-25 14:45:27 +01:00
:param path: path for saving
:param friend_number: friend number
:param file_number: file number
:param size: file size
2016-04-13 23:46:28 +02:00
:param inline: is inline image
2016-03-25 14:45:27 +01:00
"""
2016-04-12 15:11:10 +02:00
if not inline:
rt = ReceiveTransfer(path, self._tox, friend_number, size, file_number)
else:
rt = ReceiveToBuffer(self._tox, friend_number, size, file_number)
2016-03-19 12:41:01 +01:00
self._file_transfers[(friend_number, file_number)] = rt
self._tox.file_control(friend_number, file_number, TOX_FILE_CONTROL['RESUME'])
2016-03-29 14:54:58 +02:00
if item is not None:
rt.set_state_changed_handler(item.update)
2016-04-12 15:11:10 +02:00
self.get_friend_by_number(friend_number).update_transfer_data(file_number,
TOX_FILE_TRANSFER_STATE['RUNNING'])
2016-03-18 22:28:53 +01:00
def send_screenshot(self, data):
2016-03-24 12:01:07 +01:00
"""
Send screenshot to current active friend
2016-03-29 14:54:58 +02:00
:param data: raw data - png
2016-03-24 12:01:07 +01:00
"""
2016-06-14 22:55:41 +02:00
self.send_inline(data, 'toxygen_inline.png')
def send_sticker(self, path):
2016-06-21 13:58:11 +02:00
with open(path, 'rb') as fl:
2016-06-14 22:55:41 +02:00
data = fl.read()
self.send_inline(data, 'sticker.png')
def send_inline(self, data, file_name, friend_number=None, is_resend=False):
friend_number = friend_number or self.get_active_number()
friend = self.get_friend_by_number(friend_number)
if friend.status is None and not is_resend:
m = UnsentFile(file_name, data, time.time())
friend.append_message(m)
self.update()
2016-06-14 22:55:41 +02:00
return
elif friend.status is None and is_resend:
2016-06-15 15:15:23 +02:00
raise RuntimeError()
2016-06-14 22:55:41 +02:00
st = SendFromBuffer(self._tox, friend.number, data, file_name)
2016-03-29 14:54:58 +02:00
self._file_transfers[(friend.number, st.get_file_number())] = st
tm = TransferMessage(MESSAGE_OWNER['ME'],
time.time(),
TOX_FILE_TRANSFER_STATE['OUTGOING_NOT_STARTED'],
2016-03-29 14:54:58 +02:00
len(data),
2016-06-15 15:15:23 +02:00
file_name,
2016-03-29 14:54:58 +02:00
friend.number,
st.get_file_number())
item = self.create_file_transfer_item(tm)
friend.append_message(tm)
st.set_state_changed_handler(item.update)
2016-03-29 16:11:30 +02:00
self._messages.scrollToBottom()
def send_file(self, path, number=None, is_resend=False):
2016-03-24 12:01:07 +01:00
"""
Send file to current active friend
:param path: file path
2016-05-28 12:06:13 +02:00
:param number: friend_number
2016-06-15 15:15:23 +02:00
:param is_resend: is 'offline' message
2016-03-24 12:01:07 +01:00
"""
2016-05-28 12:06:13 +02:00
friend_number = number or self.get_active_number()
friend = self.get_friend_by_number(friend_number)
if friend.status is None and not is_resend:
m = UnsentFile(path, None, time.time())
friend.append_message(m)
self.update()
2016-05-28 12:06:13 +02:00
return
elif friend.status is None and is_resend:
2016-06-21 13:58:11 +02:00
print('Error in sending')
2016-06-15 15:15:23 +02:00
raise RuntimeError()
2016-03-17 21:49:27 +01:00
st = SendTransfer(path, self._tox, friend_number)
self._file_transfers[(friend_number, st.get_file_number())] = st
2016-03-29 14:54:58 +02:00
tm = TransferMessage(MESSAGE_OWNER['ME'],
time.time(),
TOX_FILE_TRANSFER_STATE['OUTGOING_NOT_STARTED'],
2016-03-29 14:54:58 +02:00
os.path.getsize(path),
os.path.basename(path),
friend_number,
st.get_file_number())
item = self.create_file_transfer_item(tm)
2016-03-21 18:19:13 +01:00
st.set_state_changed_handler(item.update)
2016-03-29 14:54:58 +02:00
self._friends[self._active_friend].append_message(tm)
2016-03-29 16:11:30 +02:00
self._messages.scrollToBottom()
2016-03-17 21:49:27 +01:00
2016-03-25 14:45:27 +01:00
def incoming_chunk(self, friend_number, file_number, position, data):
2016-06-06 20:18:32 +02:00
"""
Incoming chunk
"""
2016-03-25 14:45:27 +01:00
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 not in ACTIVE_FILE_TRANSFERS: # finished or cancelled
2016-03-25 14:45:27 +01:00
if type(transfer) is ReceiveAvatar:
self.get_friend_by_number(friend_number).load_avatar()
self.set_active(None)
2016-06-07 11:27:58 +02:00
elif type(transfer) is ReceiveToBuffer: # inline image
print('inline')
2016-04-13 23:46:28 +02:00
inline = InlineImage(transfer.get_data())
i = self.get_friend_by_number(friend_number).update_transfer_data(file_number,
TOX_FILE_TRANSFER_STATE['FINISHED'],
inline)
if friend_number == self.get_active_number():
count = self._messages.count()
if count + i + 1 >= 0:
elem = QtGui.QListWidgetItem()
item = InlineImageItem(transfer.get_data(), self._messages.width(), elem)
elem.setSizeHint(QtCore.QSize(self._messages.width(), item.height()))
self._messages.insertItem(count + i + 1, elem)
self._messages.setItemWidget(elem, item)
2016-03-29 14:54:58 +02:00
else:
self.get_friend_by_number(friend_number).update_transfer_data(file_number,
TOX_FILE_TRANSFER_STATE['FINISHED'])
2016-03-25 14:45:27 +01:00
del self._file_transfers[(friend_number, file_number)]
2016-03-17 21:49:27 +01:00
def outgoing_chunk(self, friend_number, file_number, position, size):
2016-06-06 20:18:32 +02:00
"""
2016-06-07 11:27:58 +02:00
Outgoing chunk
2016-06-06 20:18:32 +02:00
"""
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 not in ACTIVE_FILE_TRANSFERS: # finished or cancelled
del self._file_transfers[(friend_number, file_number)]
2016-03-29 14:54:58 +02:00
if type(transfer) is not SendAvatar:
2016-04-14 10:29:59 +02:00
if type(transfer) is SendFromBuffer and Settings.get_instance()['allow_inline']: # inline
2016-04-13 23:46:28 +02:00
inline = InlineImage(transfer.get_data())
print('inline')
i = self.get_friend_by_number(friend_number).update_transfer_data(file_number,
TOX_FILE_TRANSFER_STATE[
'FINISHED'],
inline)
if friend_number == self.get_active_number():
count = self._messages.count()
if count + i + 1 >= 0:
elem = QtGui.QListWidgetItem()
item = InlineImageItem(transfer.get_data(), self._messages.width(), elem)
elem.setSizeHint(QtCore.QSize(self._messages.width(), item.height()))
self._messages.insertItem(count + i + 1, elem)
self._messages.setItemWidget(elem, item)
2016-04-13 23:46:28 +02:00
else:
self.get_friend_by_number(friend_number).update_transfer_data(file_number,
TOX_FILE_TRANSFER_STATE['FINISHED'])
2016-03-24 12:01:07 +01:00
# -----------------------------------------------------------------------------------------------------------------
# Avatars support
# -----------------------------------------------------------------------------------------------------------------
def send_avatar(self, friend_number):
"""
:param friend_number: number of friend who should get new avatar
"""
avatar_path = (ProfileHelper.get_path() + 'avatars/{}.png').format(self._tox_id[:TOX_PUBLIC_KEY_SIZE * 2])
if not os.path.isfile(avatar_path): # reset image
avatar_path = None
sa = SendAvatar(avatar_path, self._tox, friend_number)
self._file_transfers[(friend_number, sa.get_file_number())] = sa
def incoming_avatar(self, friend_number, file_number, size):
"""
Friend changed avatar
:param friend_number: friend number
:param file_number: file number
:param size: size of avatar or 0 (default avatar)
"""
ra = ReceiveAvatar(self._tox, friend_number, size, file_number)
if ra.state != TOX_FILE_TRANSFER_STATE['CANCELLED']:
2016-03-24 12:01:07 +01:00
self._file_transfers[(friend_number, file_number)] = ra
else:
self.get_friend_by_number(friend_number).load_avatar()
if self.get_active_number() == friend_number:
self.set_active(None)
2016-03-18 17:33:54 +01:00
def reset_avatar(self):
super(Profile, self).reset_avatar()
for friend in filter(lambda x: x.status is not None, self._friends):
self.send_avatar(friend.number)
def set_avatar(self, data):
super(Profile, self).set_avatar(data)
for friend in filter(lambda x: x.status is not None, self._friends):
self.send_avatar(friend.number)
2016-03-14 20:30:51 +01:00
2016-04-24 12:45:11 +02:00
# -----------------------------------------------------------------------------------------------------------------
# AV support
# -----------------------------------------------------------------------------------------------------------------
def get_call(self):
return self._call
call = property(get_call)
def call_click(self, audio=True, video=False):
"""User clicked audio button in main window"""
num = self.get_active_number()
if num not in self._call and self.is_active_online(): # start call
self._call(num, audio, video)
self._screen.active_call()
2016-06-13 18:28:17 +02:00
if video:
text = QtGui.QApplication.translate("incoming_call", "Outgoing video call", None,
QtGui.QApplication.UnicodeUTF8)
else:
text = QtGui.QApplication.translate("incoming_call", "Outgoing audio call", None,
QtGui.QApplication.UnicodeUTF8)
self._friends[self._active_friend].append_message(InfoMessage(text, time.time()))
self.create_message_item(text, time.time(), '', MESSAGE_TYPE['INFO_MESSAGE'])
2016-06-13 18:28:17 +02:00
self._messages.scrollToBottom()
2016-04-24 12:45:11 +02:00
elif num in self._call: # finish or cancel call if you call with active friend
self.stop_call(num, False)
def incoming_call(self, audio, video, friend_number):
2016-04-28 10:30:58 +02:00
"""
Incoming call from friend. Only audio is supported now
"""
2016-04-24 12:45:11 +02:00
friend = self.get_friend_by_number(friend_number)
2016-06-13 18:28:17 +02:00
if video:
text = QtGui.QApplication.translate("incoming_call", "Incoming video call", None,
QtGui.QApplication.UnicodeUTF8)
else:
text = QtGui.QApplication.translate("incoming_call", "Incoming audio call", None,
QtGui.QApplication.UnicodeUTF8)
friend.append_message(InfoMessage(text, time.time()))
2016-04-24 13:09:29 +02:00
self._incoming_calls.add(friend_number)
2016-04-24 12:45:11 +02:00
if friend_number == self.get_active_number():
self._screen.incoming_call()
self.create_message_item(text, time.time(), '', MESSAGE_TYPE['INFO_MESSAGE'])
2016-06-13 18:28:17 +02:00
self._messages.scrollToBottom()
2016-04-24 12:45:11 +02:00
else:
2016-06-16 14:48:29 +02:00
friend.actions = True
2016-04-24 12:45:11 +02:00
self._call_widget = avwidgets.IncomingCallWidget(friend_number, text, friend.name)
self._call_widget.set_pixmap(friend.get_pixmap())
self._call_widget.show()
def accept_call(self, friend_number, audio, video):
2016-04-28 10:30:58 +02:00
"""
Accept incoming call with audio or video
"""
2016-04-24 12:45:11 +02:00
self._call.accept_call(friend_number, audio, video)
self._screen.active_call()
2016-04-24 13:09:29 +02:00
self._incoming_calls.remove(friend_number)
2016-04-24 12:45:11 +02:00
if hasattr(self, '_call_widget'):
del self._call_widget
def stop_call(self, friend_number, by_friend):
2016-04-28 10:30:58 +02:00
"""
Stop call with friend
"""
2016-04-24 13:09:29 +02:00
if friend_number in self._incoming_calls:
self._incoming_calls.remove(friend_number)
2016-06-13 18:28:17 +02:00
text = QtGui.QApplication.translate("incoming_call", "Call declined", None, QtGui.QApplication.UnicodeUTF8)
else:
text = QtGui.QApplication.translate("incoming_call", "Call finished", None, QtGui.QApplication.UnicodeUTF8)
2016-04-24 12:45:11 +02:00
self._screen.call_finished()
self._call.finish_call(friend_number, by_friend) # finish or decline call
if hasattr(self, '_call_widget'):
del self._call_widget
2016-06-13 18:28:17 +02:00
friend = self.get_friend_by_number(friend_number)
friend.append_message(InfoMessage(text, time.time()))
if friend_number == self.get_active_number():
self.create_message_item(text, time.time(), '', MESSAGE_TYPE['INFO_MESSAGE'])
2016-06-13 18:28:17 +02:00
self._messages.scrollToBottom()
2016-04-24 12:45:11 +02:00
2016-02-20 19:21:56 +01:00
2016-02-23 22:03:50 +01:00
def tox_factory(data=None, settings=None):
2016-02-26 15:32:36 +01:00
"""
:param data: user data from .tox file. None = no saved data, create new profile
2016-04-14 14:01:59 +02:00
:param settings: current profile settings. None = default settings will be used
2016-02-26 15:32:36 +01:00
:return: new tox instance
"""
2016-02-23 22:03:50 +01:00
if settings is None:
settings = Settings.get_default_settings()
2016-02-20 19:21:56 +01:00
tox_options = Tox.options_new()
tox_options.contents.udp_enabled = settings['udp_enabled']
tox_options.contents.proxy_type = settings['proxy_type']
2016-06-21 13:58:11 +02:00
tox_options.contents.proxy_host = bytes(settings['proxy_host'], 'UTF-8')
2016-02-20 19:21:56 +01:00
tox_options.contents.proxy_port = settings['proxy_port']
tox_options.contents.start_port = settings['start_port']
tox_options.contents.end_port = settings['end_port']
tox_options.contents.tcp_port = settings['tcp_port']
2016-02-23 22:03:50 +01:00
if data: # load existing profile
tox_options.contents.savedata_type = TOX_SAVEDATA_TYPE['TOX_SAVE']
tox_options.contents.savedata_data = c_char_p(data)
tox_options.contents.savedata_length = len(data)
2016-02-25 12:22:15 +01:00
else: # create new profile
2016-02-23 22:03:50 +01:00
tox_options.contents.savedata_type = TOX_SAVEDATA_TYPE['NONE']
tox_options.contents.savedata_data = None
tox_options.contents.savedata_length = 0
2016-02-20 19:21:56 +01:00
return Tox(tox_options)