toxygen/src/profile.py

986 lines
42 KiB
Python
Raw Normal View History

2016-03-18 22:28:53 +01:00
from list_items import MessageItem, ContactItem, FileTransferItem
2016-02-26 19:54:15 +01:00
from PySide import QtCore, QtGui
2016-02-20 19:21:56 +01:00
from tox import Tox
2016-03-12 21:18:13 +01:00
import os
2016-03-29 14:54:58 +02:00
from messages import *
2016-03-12 21:18:13 +01:00
from settings import Settings
2016-02-20 19:21:56 +01:00
from toxcore_enums_and_consts import *
from ctypes import *
2016-03-13 13:06:06 +01:00
from util import curr_time, log, Singleton, curr_directory, convert_time
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-02-18 17:15:38 +01:00
2016-02-25 21:40:00 +01:00
class ProfileHelper(object):
2016-02-26 15:32:36 +01:00
"""
Class with static methods for search, load and save profiles
"""
2016-02-18 17:15:38 +01:00
@staticmethod
def find_profiles():
path = Settings.get_default_path()
result = []
# check default path
2016-03-10 22:12:41 +01:00
if not os.path.exists(path):
os.makedirs(path)
2016-02-18 17:15:38 +01:00
for fl in os.listdir(path):
if fl.endswith('.tox'):
2016-02-18 17:15:38 +01:00
name = fl[:-4]
result.append((path, name))
2016-03-10 22:12:41 +01:00
path = curr_directory()
2016-02-18 17:15:38 +01:00
# check current directory
for fl in os.listdir(path):
if fl.endswith('.tox'):
2016-02-18 17:15:38 +01:00
name = fl[:-4]
2016-03-10 22:12:41 +01:00
result.append((path + '/', name))
2016-02-18 17:15:38 +01:00
return result
@staticmethod
def open_profile(path, name):
2016-02-25 21:40:00 +01:00
ProfileHelper._path = path + name + '.tox'
2016-03-16 09:01:23 +01:00
ProfileHelper._directory = path
2016-02-25 21:40:00 +01:00
with open(ProfileHelper._path, 'rb') as fl:
data = fl.read()
if data:
2016-02-25 21:40:00 +01:00
print 'Data loaded from: {}'.format(ProfileHelper._path)
return data
else:
2016-02-25 21:40:00 +01:00
raise IOError('Save file not found. Path: {}'.format(ProfileHelper._path))
@staticmethod
2016-02-25 10:35:42 +01:00
def save_profile(data, name=None):
if name is not None:
2016-02-25 21:40:00 +01:00
ProfileHelper._path = Settings.get_default_path() + name + '.tox'
with open(ProfileHelper._path, 'wb') as fl:
fl.write(data)
2016-02-25 21:40:00 +01:00
print 'Data saved to: {}'.format(ProfileHelper._path)
@staticmethod
def export_profile(new_path):
new_path += os.path.basename(ProfileHelper._path)
with open(ProfileHelper._path, 'rb') as fin:
data = fin.read()
with open(new_path, 'wb') as fout:
fout.write(data)
print 'Data exported to: {}'.format(new_path)
2016-03-16 09:01:23 +01:00
@staticmethod
def get_path():
return ProfileHelper._directory
2016-02-25 21:40:00 +01:00
class Contact(object):
2016-02-26 15:32:36 +01:00
"""
Class encapsulating TOX contact
Properties: name (alias of contact or name), status_message, status (connection status)
2016-03-09 20:46:00 +01:00
widget - widget for update
2016-02-26 15:32:36 +01:00
"""
2016-02-25 21:40:00 +01:00
def __init__(self, name, status_message, widget, tox_id):
2016-03-09 20:46:00 +01:00
"""
:param name: name, example: 'Toxygen user'
:param status_message: status message, example: 'Toxing on toxygen'
:param widget: ContactItem instance
:param tox_id: tox id of contact
"""
self._name, self._status_message = name, status_message
2016-02-25 21:40:00 +01:00
self._status, self._widget = None, widget
2016-03-22 10:50:18 +01:00
if type(self) is Profile:
2016-03-30 19:33:42 +02:00
self._widget.name.setText(name if len(name) <= 12 else name[:9] + '...')
2016-03-22 10:50:18 +01:00
else:
self._widget.name.setText(name if len(name) <= 20 else name[:17] + '...')
if type(self) is Profile:
text = self._status_message if len(self._status_message) <= 20 else self._status_message[:17] + '...'
else:
text = self._status_message if len(self._status_message) <= 30 else self._status_message[:27] + '...'
self._widget.status_message.setText(text)
self._tox_id = tox_id
self.load_avatar()
2016-02-25 21:40:00 +01:00
# -----------------------------------------------------------------------------------------------------------------
# name - current name or alias of user
# -----------------------------------------------------------------------------------------------------------------
def get_name(self):
2016-02-25 21:40:00 +01:00
return self._name
def set_name(self, value):
self._name = value.decode('utf-8')
2016-03-22 10:50:18 +01:00
if type(self) is Profile:
2016-03-30 19:33:42 +02:00
self._widget.name.setText(self._name if len(self._name) <= 12 else self._name[:9] + '...')
2016-03-22 10:50:18 +01:00
else:
self._widget.name.setText(self._name if len(self._name) <= 20 else self._name[:17] + '...')
self._widget.name.repaint()
2016-02-25 21:40:00 +01:00
name = property(get_name, set_name)
2016-02-25 21:40:00 +01:00
# -----------------------------------------------------------------------------------------------------------------
# Status message
# -----------------------------------------------------------------------------------------------------------------
def get_status_message(self):
2016-02-25 21:40:00 +01:00
return self._status_message
def set_status_message(self, value):
self._status_message = value.decode('utf-8')
2016-03-22 10:50:18 +01:00
if type(self) is Profile:
text = self._status_message if len(self._status_message) <= 20 else self._status_message[:17] + '...'
else:
text = self._status_message if len(self._status_message) <= 30 else self._status_message[:27] + '...'
self._widget.status_message.setText(text)
self._widget.status_message.repaint()
2016-02-25 21:40:00 +01:00
status_message = property(get_status_message, set_status_message)
2016-02-25 21:40:00 +01:00
# -----------------------------------------------------------------------------------------------------------------
# Status
# -----------------------------------------------------------------------------------------------------------------
def get_status(self):
2016-02-25 21:40:00 +01:00
return self._status
def set_status(self, value):
self._widget.connection_status.data = self._status = value
self._widget.connection_status.repaint()
2016-02-25 21:40:00 +01:00
status = property(get_status, set_status)
2016-02-25 21:40:00 +01:00
# -----------------------------------------------------------------------------------------------------------------
# TOX ID. WARNING: for friend it will return public key, for profile - full address
# -----------------------------------------------------------------------------------------------------------------
def get_tox_id(self):
return self._tox_id
tox_id = property(get_tox_id)
# -----------------------------------------------------------------------------------------------------------------
# Avatars
# -----------------------------------------------------------------------------------------------------------------
def load_avatar(self):
2016-03-09 20:46:00 +01:00
"""
Tries to load avatar of contact or uses default avatar
"""
2016-03-24 06:36:59 +01:00
avatar_path = '{}.png'.format(self._tox_id[:TOX_PUBLIC_KEY_SIZE * 2])
os.chdir(ProfileHelper.get_path() + 'avatars/')
if not os.path.isfile(avatar_path): # load default image
2016-03-24 06:36:59 +01:00
avatar_path = 'avatar.png'
os.chdir(curr_directory() + '/images/')
pixmap = QtGui.QPixmap(QtCore.QSize(64, 64))
pixmap.scaled(64, 64, QtCore.Qt.KeepAspectRatio)
self._widget.avatar_label.setPixmap(avatar_path)
self._widget.avatar_label.repaint()
2016-03-11 12:37:45 +01:00
def reset_avatar(self):
2016-03-18 14:20:07 +01:00
avatar_path = (ProfileHelper.get_path() + 'avatars/{}.png').format(self._tox_id[:TOX_PUBLIC_KEY_SIZE * 2])
2016-03-11 12:37:45 +01:00
if os.path.isfile(avatar_path):
os.remove(avatar_path)
self.load_avatar()
def set_avatar(self, avatar):
2016-03-18 14:20:07 +01:00
avatar_path = (ProfileHelper.get_path() + 'avatars/{}.png').format(self._tox_id[:TOX_PUBLIC_KEY_SIZE * 2])
2016-03-11 12:37:45 +01:00
with open(avatar_path, 'wb') as f:
f.write(avatar)
self.load_avatar()
2016-02-25 21:40:00 +01:00
class Friend(Contact):
2016-02-26 15:32:36 +01:00
"""
2016-03-09 20:46:00 +01:00
Friend in list of friends. Can be hidden, properties 'has unread messages' and 'has alias' added
2016-02-26 15:32:36 +01:00
"""
2016-02-25 21:40:00 +01:00
2016-03-12 21:18:13 +01:00
def __init__(self, message_getter, number, *args):
2016-03-09 20:46:00 +01:00
"""
2016-03-13 13:06:06 +01:00
:param message_getter: gets messages from db
2016-03-09 20:46:00 +01:00
:param number: number of friend.
"""
2016-02-25 21:40:00 +01:00
super(Friend, self).__init__(*args)
self._number = number
2016-02-25 21:40:00 +01:00
self._new_messages = False
self._visible = True
self._alias = False
2016-03-12 21:18:13 +01:00
self._message_getter = message_getter
self._corr = []
self._unsaved_messages = 0
2016-03-13 13:06:06 +01:00
self._history_loaded = False
def __del__(self):
self.set_visibility(False)
del self._widget
2016-03-15 21:35:15 +01:00
del self._message_getter
2016-03-12 21:18:13 +01:00
# -----------------------------------------------------------------------------------------------------------------
# History support
# -----------------------------------------------------------------------------------------------------------------
2016-03-13 13:06:06 +01:00
def load_corr(self, first_time=True):
"""
:param first_time: friend became active, load first part of messages
2016-03-24 22:30:26 +01:00
:return: list of loaded messages
2016-03-13 13:06:06 +01:00
"""
2016-03-24 12:01:07 +01:00
if (first_time and self._history_loaded) or (not hasattr(self, '_message_getter')):
2016-03-13 13:06:06 +01:00
return
2016-03-29 14:54:58 +02:00
data = self._message_getter.get(PAGE_SIZE)
2016-03-12 21:18:13 +01:00
if data is not None and len(data):
data.reverse()
else:
2016-03-24 22:15:07 +01:00
return []
2016-03-29 16:11:30 +02:00
data = map(lambda tupl: TextMessage(*tupl), data)
self._corr = data + self._corr
2016-03-13 13:06:06 +01:00
self._history_loaded = True
2016-03-24 22:15:07 +01:00
return data
2016-03-12 21:18:13 +01:00
def get_corr_for_saving(self):
2016-03-13 13:06:06 +01:00
"""
Get data to save in db
:return: list of unsaved messages or []
"""
2016-03-24 12:01:07 +01:00
if hasattr(self, '_message_getter'):
del self._message_getter
2016-03-29 14:54:58 +02:00
messages = filter(lambda x: x.get_type() <= 1, self._corr)
return map(lambda x: x.get_data(), messages[-self._unsaved_messages:]) if self._unsaved_messages else []
2016-03-12 21:18:13 +01:00
def get_corr(self):
return self._corr
def append_message(self, message):
2016-03-13 13:06:06 +01:00
"""
:param message: tuple (message, owner, unix_time, message_type)
"""
2016-03-12 21:18:13 +01:00
self._corr.append(message)
2016-03-29 14:54:58 +02:00
if message.get_type() <= 1:
self._unsaved_messages += 1
2016-03-12 21:18:13 +01:00
def clear_corr(self):
2016-03-13 13:06:06 +01:00
"""
Clear messages list
"""
2016-03-24 12:01:07 +01:00
if hasattr(self, '_message_getter'):
del self._message_getter
2016-03-29 16:11:30 +02:00
self._corr = filter(lambda x: x.get_type() > 1, self._corr)
2016-03-29 14:54:58 +02:00
self._unsaved_messages = 0
def update_transfer_data(self, file_number, status):
"""
Update status of active transfer
"""
try:
tr = filter(lambda x: x.get_type() == 2 and x.is_active(file_number), self._corr)[0]
tr.set_status(status)
except:
pass
2016-03-12 21:18:13 +01:00
# -----------------------------------------------------------------------------------------------------------------
# Alias support
# -----------------------------------------------------------------------------------------------------------------
def set_name(self, value):
2016-03-13 13:06:06 +01:00
"""
Set new name or ignore if alias exists
:param value: new name
"""
if not self._alias:
super(self.__class__, self).set_name(value)
def set_alias(self, alias):
self._alias = bool(alias)
# -----------------------------------------------------------------------------------------------------------------
# Visibility in friends' list
# -----------------------------------------------------------------------------------------------------------------
def get_visibility(self):
return self._visible
2016-02-25 21:40:00 +01:00
def set_visibility(self, value):
self._visible = value
visibility = property(get_visibility, set_visibility)
2016-02-25 21:40:00 +01:00
# -----------------------------------------------------------------------------------------------------------------
# Unread messages from friend
# -----------------------------------------------------------------------------------------------------------------
def get_messages(self):
return self._new_messages
def set_messages(self, value):
self._widget.connection_status.messages = self._new_messages = value
self._widget.connection_status.repaint()
2016-02-25 21:40:00 +01:00
messages = property(get_messages, set_messages)
2016-02-25 21:40:00 +01:00
# -----------------------------------------------------------------------------------------------------------------
# Friend's number (can be used in toxcore)
# -----------------------------------------------------------------------------------------------------------------
def get_number(self):
2016-02-25 21:40:00 +01:00
return self._number
def set_number(self, value):
self._number = value
number = property(get_number, set_number)
2016-02-25 21:40:00 +01:00
2016-03-06 13:44:52 +01:00
class Profile(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-03-13 13:06:06 +01:00
super(Profile, self).__init__(tox.self_get_name(),
tox.self_get_status_message(),
screen.user_info,
tox.self_get_address())
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)
settings = Settings.get_instance()
2016-03-16 09:01:23 +01:00
self._show_online = settings['show_online_friends']
screen.online_contacts.setChecked(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)
2016-03-15 21:54:01 +01:00
if not self._history.friend_exists_in_db(tox_id):
self._history.add_friend_to_db(tox_id)
try:
alias = 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-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
# -----------------------------------------------------------------------------------------------------------------
# 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:
status = (self._status + 1) % 3
super(self.__class__, self).set_status(status)
2016-03-16 09:01:23 +01:00
self._tox.self_set_status(status)
def set_name(self, value):
super(self.__class__, self).set_name(value)
2016-03-16 09:01:23 +01:00
self._tox.self_set_name(self._name.encode('utf-8'))
def set_status_message(self, value):
super(self.__class__, 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'))
# -----------------------------------------------------------------------------------------------------------------
# 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-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):
return filter(lambda x: x.number == num, self._friends)[0]
# -----------------------------------------------------------------------------------------------------------------
# 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):
"""
: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
2016-03-13 13:06:06 +01:00
if value == self._active_friend:
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-03-09 19:11:36 +01:00
if value is not None:
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-03-09 19:11:36 +01:00
self._friends[self._active_friend].set_messages(False)
2016-03-16 09:01:23 +01:00
self._screen.messageEdit.clear()
2016-03-15 18:05:19 +01:00
self._messages.clear()
2016-03-13 13:06:06 +01:00
friend.load_corr()
2016-03-24 22:30:26 +01:00
messages = friend.get_corr()
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],
convert_time(data[2]),
friend.name if data[1] else self._name,
data[3])
elif message.get_type() == 2:
item = self.create_file_transfer_item(message)
if message.get_status() in (2, 4):
ft = self._file_transfers[(message.get_friend_number(), message.get_file_number())]
ft.set_state_changed_handler(item.update)
2016-03-15 18:05:19 +01:00
self._messages.scrollToBottom()
2016-03-13 13:06:06 +01:00
else:
friend = self._friends[self._active_friend]
name = friend.name if len(friend.name) < 50 else friend.name[:47] + '...'
status_message = friend.status_message if len(friend.status_message) < 66 else friend.status_message[:63] + '...'
self._screen.account_name.setText(name)
self._screen.account_status.setText(status_message)
2016-03-09 19:11:36 +01:00
avatar_path = (Settings.get_default_path() + 'avatars/{}.png').format(friend.tox_id[:TOX_PUBLIC_KEY_SIZE * 2])
if not os.path.isfile(avatar_path): # load default image
avatar_path = curr_directory() + '/images/avatar.png'
pixmap = QtGui.QPixmap(QtCore.QSize(64, 64))
pixmap.scaled(64, 64, QtCore.Qt.KeepAspectRatio)
2016-03-16 09:01:23 +01:00
self._screen.account_avatar.setPixmap(avatar_path)
self._screen.account_avatar.repaint()
except: # no friend found. ignore
log('Incorrect friend value: ' + str(value))
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_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
# -----------------------------------------------------------------------------------------------------------------
# Private messages
# -----------------------------------------------------------------------------------------------------------------
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
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
user_name = Profile.get_instance().get_active_name()
2016-03-12 21:18:13 +01:00
self.create_message_item(message.decode('utf-8'), curr_time(), user_name, 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.decode('utf-8'), MESSAGE_OWNER['FRIEND'], time.time(), 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)
friend.set_messages(True)
2016-03-29 14:54:58 +02:00
friend.append_message(
TextMessage(message.decode('utf-8'), MESSAGE_OWNER['FRIEND'], time.time(), message_type))
2016-02-26 19:54:15 +01:00
def send_message(self, text):
"""
Send message to active friend
:param text: message text
"""
if self.is_active_online() and text:
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-03-16 16:15:55 +01:00
self.split_and_send(friend.number, message_type, text.encode('utf-8'))
2016-03-12 21:18:13 +01:00
self.create_message_item(text, curr_time(), self._name, 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()
2016-03-29 14:54:58 +02:00
friend.append_message(TextMessage(text, MESSAGE_OWNER['ME'], time.time(), message_type))
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
"""
2016-03-18 14:50:32 +01:00
if hasattr(self, '_history'):
if Settings.get_instance()['save_history']:
for friend in self._friends:
messages = friend.get_corr_for_saving()
2016-03-22 22:21:14 +01:00
if not self._history.friend_exists_in_db(friend.tox_id):
self._history.add_friend_to_db(friend.tox_id)
2016-03-18 14:50:32 +01:00
self._history.save_messages_to_db(friend.tox_id, messages)
del self._history
2016-03-13 13:06:06 +01:00
def clear_history(self, num=None):
if num is not None:
friend = self._friends[num]
friend.clear_corr()
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-03-22 22:21:14 +01:00
for number in xrange(len(self._friends)):
self.clear_history(number)
2016-03-13 13:06:06 +01:00
if num is None or num == self.get_active_number():
self._messages.clear()
2016-03-22 22:21:14 +01:00
self._messages.repaint()
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]
data = friend.load_corr(False)
if not data:
return
data.reverse()
2016-03-24 22:15:07 +01:00
for message in data:
2016-03-29 16:11:30 +02:00
if message.get_type() <= 1:
data = message.get_data()
self.create_message_item(data[0],
convert_time(data[2]),
friend.name if data[1] else self._name,
data[3],
False)
elif message.get_type() == 2:
item = self.create_file_transfer_item(message, False)
if message.get_status() in (2, 4):
ft = self._file_transfers[(message.get_friend_number(), message.get_file_number())]
ft.set_state_changed_handler(item.update)
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-03-24 22:15:07 +01:00
def create_message_item(self, text, time, name, message_type, append=True):
2016-03-12 21:18:13 +01:00
item = MessageItem(text, time, name, 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-12 21:18:13 +01:00
self._messages.repaint()
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
item = FileTransferItem(*data)
elem = QtGui.QListWidgetItem()
elem.setSizeHint(QtCore.QSize(600, 50))
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)
self._messages.repaint()
2016-03-19 12:41:01 +01:00
return item
2016-03-18 22:28:53 +01:00
2016-03-12 21:18:13 +01:00
# -----------------------------------------------------------------------------------------------------------------
2016-03-13 13:06:06 +01:00
# Work with friends (remove, set alias, get public key)
# -----------------------------------------------------------------------------------------------------------------
2016-03-11 12:37:45 +01:00
def set_alias(self, num):
friend = self._friends[num]
name = friend.name.encode('utf-8')
2016-03-10 22:12:41 +01:00
dialog = "Enter new alias for friend " + name.decode('utf-8') + " or leave empty to use friend's name:"
text, ok = QtGui.QInputDialog.getText(None, 'Set alias', dialog)
if ok:
settings = Settings.get_instance()
aliases = settings['friends_aliases']
if text:
friend.name = text.encode('utf-8')
try:
index = 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-03-22 10:50:18 +01:00
friend.name = self._tox.friend_get_name(friend.number).encode('utf-8')
friend.set_alias('')
try:
index = map(lambda x: x[0], aliases).index(friend.tox_id)
del aliases[index]
except:
pass
settings.save()
self.set_active()
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-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)
# -----------------------------------------------------------------------------------------------------------------
# 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 'Add me to your contact list'
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')
2016-03-16 09:01:23 +01:00
result = self._tox.friend_add(tox_id, message.encode('utf-8'))
2016-03-09 20:46:00 +01:00
tox_id = tox_id[:TOX_PUBLIC_KEY_SIZE * 2]
2016-03-09 19:11:36 +01:00
item = self.create_friend_item()
2016-03-15 21:54:01 +01:00
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)
2016-03-15 20:42:24 +01:00
friend = Friend(message_getter, result, tox_id, '', item, tox_id)
2016-03-09 19:11:36 +01:00
self._friends.append(friend)
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:
info = 'User {} wants to add you to contact list. Message:\n{}'.format(tox_id, message)
reply = QtGui.QMessageBox.question(None, 'Friend request', info, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes: # accepted
2016-03-16 09:01:23 +01:00
num = self._tox.friend_add_norequest(tox_id) # num - friend number
2016-03-09 19:11:36 +01:00
item = self.create_friend_item()
2016-03-15 21:54:01 +01:00
if not self._history.friend_exists_in_db(tox_id):
self._history.add_friend_to_db(tox_id)
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)
2016-03-15 20:42:24 +01:00
friend = Friend(message_getter, num, tox_id, '', item, tox_id)
2016-03-09 19:11:36 +01:00
self._friends.append(friend)
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-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
# -----------------------------------------------------------------------------------------------------------------
# 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-03-25 14:45:27 +01:00
file_name = file_name.decode('utf-8')
2016-03-18 17:33:54 +01:00
if settings['allow_auto_accept'] and friend.tox_id in settings['auto_accept_from_friends']:
path = settings['auto_accept_path'] or 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
2016-03-29 14:54:58 +02:00
self.accept_transfer(None, path + '/' + new_file_name, friend_number, file_number)
tm = TransferMessage(MESSAGE_OWNER['FRIEND'],
time.time(),
FILE_TRANSFER_MESSAGE_STATUS['INCOMING_STARTED'],
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(),
FILE_TRANSFER_MESSAGE_STATUS['INCOMING_NOT_STARTED'],
size,
file_name,
friend_number,
file_number)
if friend_number == self.get_active_number():
self.create_file_transfer_item(tm)
2016-03-29 16:11:30 +02:00
self._messages.scrollToBottom()
2016-03-29 14:54:58 +02:00
else:
friend.set_messages(True)
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
"""
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()
2016-03-19 12:41:01 +01:00
del self._file_transfers[(friend_number, file_number)]
2016-03-29 14:54:58 +02:00
else:
self._tox.file_control(friend_number, file_number, TOX_FILE_CONTROL['CANCEL'])
self.get_friend_by_number(friend_number).update_transfer_data(file_number,
FILE_TRANSFER_MESSAGE_STATUS['CANCELLED'])
2016-03-19 12:41:01 +01:00
2016-03-21 19:53:02 +01:00
def accept_transfer(self, item, path, friend_number, file_number, size):
2016-03-25 14:45:27 +01:00
"""
:param item: transfer item
:param path: path for saving
:param friend_number: friend number
:param file_number: file number
:param size: file size
"""
2016-03-21 19:53:02 +01:00
rt = ReceiveTransfer(path, 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)
self.get_friend_by_number(friend_number).update_transfer_data(file_number, FILE_TRANSFER_MESSAGE_STATUS['INCOMING_STARTED'])
2016-03-18 22:28:53 +01:00
def send_screenshot(self, data):
2016-03-24 12:01:07 +01:00
"""
Sen 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-03-29 14:54:58 +02:00
friend = self._friends[self._active_friend]
st = SendFromBuffer(self._tox, friend.number, data, 'toxygen_inline.png')
self._file_transfers[(friend.number, st.get_file_number())] = st
tm = TransferMessage(MESSAGE_OWNER['ME'],
time.time(),
FILE_TRANSFER_MESSAGE_STATUS['OUTGOING'],
len(data),
'toxygen_inline.png',
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):
2016-03-24 12:01:07 +01:00
"""
Send file to current active friend
:param path: file path
"""
2016-03-17 21:49:27 +01:00
friend_number = self.get_active_number()
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(),
FILE_TRANSFER_MESSAGE_STATUS['OUTGOING'],
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):
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()
self.set_active(None)
2016-03-29 14:54:58 +02:00
else:
self.get_friend_by_number(friend_number).update_transfer_data(file_number, FILE_TRANSFER_MESSAGE_STATUS['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):
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)]
2016-03-29 14:54:58 +02:00
if type(transfer) is not SendAvatar:
self.get_friend_by_number(friend_number).update_transfer_data(file_number, FILE_TRANSFER_MESSAGE_STATUS['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['CANCELED']:
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-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
:param settings: current application settings. None = defaults settings will be used
: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']
tox_options.contents.proxy_host = settings['proxy_host']
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)