2018-04-18 23:55:51 +03:00
|
|
|
from contacts.friend import Friend
|
2018-05-15 17:00:12 +03:00
|
|
|
from messenger.messages import *
|
2018-03-10 18:42:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
class ContactsManager:
|
2018-05-01 21:40:29 +03:00
|
|
|
"""
|
|
|
|
Represents contacts list.
|
|
|
|
"""
|
2018-03-10 18:42:53 +03:00
|
|
|
|
2018-05-15 17:00:12 +03:00
|
|
|
def __init__(self, tox, settings, screen, profile_manager, contact_provider, history, tox_dns,
|
|
|
|
messages_items_factory):
|
2018-03-10 18:42:53 +03:00
|
|
|
self._tox = tox
|
|
|
|
self._settings = settings
|
2018-04-18 23:55:51 +03:00
|
|
|
self._screen = screen
|
|
|
|
self._profile_manager = profile_manager
|
2018-04-29 00:52:42 +03:00
|
|
|
self._contact_provider = contact_provider
|
2018-05-05 00:09:33 +03:00
|
|
|
self._tox_dns = tox_dns
|
2018-05-15 17:00:12 +03:00
|
|
|
self._messages_items_factory = messages_items_factory
|
2018-04-18 23:55:51 +03:00
|
|
|
self._messages = screen.messages
|
2018-04-29 00:52:42 +03:00
|
|
|
self._contacts, self._active_contact = [], -1
|
2018-03-10 18:42:53 +03:00
|
|
|
self._sorting = settings['sorting']
|
|
|
|
self._filter_string = ''
|
|
|
|
self._friend_item_height = 40 if settings['compact_mode'] else 70
|
|
|
|
screen.online_contacts.setCurrentIndex(int(self._sorting))
|
2018-05-15 17:00:12 +03:00
|
|
|
self._history = history
|
2018-04-29 00:52:42 +03:00
|
|
|
self._load_contacts()
|
|
|
|
|
2018-05-11 00:35:56 +03:00
|
|
|
def get_contact(self, num):
|
2018-03-10 18:42:53 +03:00
|
|
|
if num < 0 or num >= len(self._contacts):
|
|
|
|
return None
|
|
|
|
return self._contacts[num]
|
|
|
|
|
2018-04-29 00:52:42 +03:00
|
|
|
def get_curr_contact(self):
|
|
|
|
return self._contacts[self._active_contact] if self._active_contact + 1 else None
|
2018-03-10 18:42:53 +03:00
|
|
|
|
2018-04-18 23:55:51 +03:00
|
|
|
def save_profile(self):
|
|
|
|
data = self._tox.get_savedata()
|
|
|
|
self._profile_manager.save_profile(data)
|
|
|
|
|
2018-05-01 16:39:09 +03:00
|
|
|
def is_friend_active(self, friend_number):
|
2018-05-15 13:40:59 +03:00
|
|
|
if not self.is_active_a_friend():
|
2018-05-01 16:39:09 +03:00
|
|
|
return False
|
|
|
|
|
|
|
|
return self.get_curr_contact().number == friend_number
|
|
|
|
|
2018-05-19 16:00:28 +03:00
|
|
|
def is_group_active(self, group_number):
|
|
|
|
if self.is_active_a_friend():
|
|
|
|
return False
|
|
|
|
|
|
|
|
return self.get_curr_contact().number == friend_number
|
|
|
|
|
2018-03-10 18:42:53 +03:00
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
# Work with active friend
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def get_active(self):
|
2018-04-29 00:52:42 +03:00
|
|
|
return self._active_contact
|
2018-03-10 18:42:53 +03:00
|
|
|
|
2018-05-15 17:00:12 +03:00
|
|
|
def set_active(self, value):
|
2018-03-10 18:42:53 +03:00
|
|
|
"""
|
|
|
|
Change current active friend or update info
|
2018-05-15 17:00:12 +03:00
|
|
|
:param value: number of new active friend in friend's list
|
2018-03-10 18:42:53 +03:00
|
|
|
"""
|
2018-04-29 00:52:42 +03:00
|
|
|
if value is None and self._active_contact == -1: # nothing to update
|
2018-03-10 18:42:53 +03:00
|
|
|
return
|
|
|
|
if value == -1: # all friends were deleted
|
|
|
|
self._screen.account_name.setText('')
|
|
|
|
self._screen.account_status.setText('')
|
|
|
|
self._screen.account_status.setToolTip('')
|
2018-04-29 00:52:42 +03:00
|
|
|
self._active_contact = -1
|
2018-03-10 18:42:53 +03:00
|
|
|
self._screen.account_avatar.setHidden(True)
|
|
|
|
self._messages.clear()
|
|
|
|
self._screen.messageEdit.clear()
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
self._screen.typing.setVisible(False)
|
2018-05-05 00:09:33 +03:00
|
|
|
current_contact = self.get_curr_contact()
|
|
|
|
if current_contact is not None:
|
2018-05-17 00:02:22 +03:00
|
|
|
current_contact.typing_notification_handler.send(self._tox, False)
|
2018-05-15 17:00:12 +03:00
|
|
|
current_contact.remove_messages_widgets() # TODO: if required
|
2018-05-05 00:09:33 +03:00
|
|
|
self._unsubscribe_from_events(current_contact)
|
2018-05-15 17:00:12 +03:00
|
|
|
|
|
|
|
if self._active_contact + 1 and self._active_contact != value:
|
|
|
|
try:
|
|
|
|
current_contact.curr_text = self._screen.messageEdit.toPlainText()
|
|
|
|
except:
|
|
|
|
pass
|
2018-05-16 19:04:02 +03:00
|
|
|
contact = self._contacts[value]
|
|
|
|
self._subscribe_to_events(contact)
|
|
|
|
contact.remove_invalid_unsent_files()
|
2018-05-15 17:00:12 +03:00
|
|
|
if self._active_contact != value:
|
2018-05-16 19:04:02 +03:00
|
|
|
self._screen.messageEdit.setPlainText(contact.curr_text)
|
2018-05-15 17:00:12 +03:00
|
|
|
self._active_contact = value
|
2018-05-16 19:04:02 +03:00
|
|
|
contact.reset_messages()
|
2018-05-15 17:00:12 +03:00
|
|
|
if not self._settings['save_history']:
|
2018-05-16 19:04:02 +03:00
|
|
|
contact.delete_old_messages()
|
2018-05-15 17:00:12 +03:00
|
|
|
self._messages.clear()
|
2018-05-16 19:04:02 +03:00
|
|
|
contact.load_corr()
|
|
|
|
corr = contact.get_corr()[-PAGE_SIZE:]
|
2018-05-15 17:00:12 +03:00
|
|
|
for message in corr:
|
2018-05-17 00:02:22 +03:00
|
|
|
if message.type == MESSAGE_TYPE['FILE_TRANSFER']:
|
|
|
|
self._messages_items_factory.create_file_transfer_item(message)
|
|
|
|
elif message.type == MESSAGE_TYPE['INLINE']:
|
|
|
|
self._messages_items_factory.create_inline_item(message.data)
|
|
|
|
else:
|
|
|
|
self._messages_items_factory.create_message_item(message)
|
2018-05-15 17:00:12 +03:00
|
|
|
# if value in self._call:
|
|
|
|
# self._screen.active_call()
|
|
|
|
# elif value in self._incoming_calls:
|
|
|
|
# self._screen.incoming_call()
|
|
|
|
# else:
|
|
|
|
# self._screen.call_finished()
|
2018-05-16 19:04:02 +03:00
|
|
|
self._set_current_contact_data(contact)
|
2018-05-05 00:09:33 +03:00
|
|
|
|
2018-03-10 18:42:53 +03:00
|
|
|
except Exception as ex: # no friend found. ignore
|
2018-04-18 23:55:51 +03:00
|
|
|
util.log('Friend value: ' + str(value))
|
|
|
|
util.log('Error in set active: ' + str(ex))
|
2018-03-10 18:42:53 +03:00
|
|
|
raise
|
|
|
|
|
2018-05-15 13:40:59 +03:00
|
|
|
active_friend = property(get_active, set_active)
|
|
|
|
|
2018-05-15 17:00:12 +03:00
|
|
|
def set_active_by_number_and_type(self, number, is_friend): # TODO: by id
|
2018-03-10 18:42:53 +03:00
|
|
|
for i in range(len(self._contacts)):
|
|
|
|
c = self._contacts[i]
|
|
|
|
if c.number == number and (type(c) is Friend == is_friend):
|
2018-04-29 00:52:42 +03:00
|
|
|
self._active_contact = i
|
2018-03-10 18:42:53 +03:00
|
|
|
break
|
|
|
|
|
2018-03-12 00:32:46 +03:00
|
|
|
def update(self):
|
2018-04-29 00:52:42 +03:00
|
|
|
if self._active_contact + 1:
|
|
|
|
self.set_active(self._active_contact)
|
2018-03-12 00:32:46 +03:00
|
|
|
|
2018-05-15 13:40:59 +03:00
|
|
|
def is_active_a_friend(self):
|
|
|
|
return type(self.get_curr_contact()) is Friend
|
|
|
|
|
2018-03-10 18:42:53 +03:00
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
# Filtration
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def filtration_and_sorting(self, sorting=0, filter_str=''):
|
|
|
|
"""
|
|
|
|
Filtration of friends list
|
|
|
|
:param sorting: 0 - no sort, 1 - online only, 2 - online first, 4 - by name
|
|
|
|
:param filter_str: show contacts which name contains this substring
|
|
|
|
"""
|
2018-05-01 21:40:29 +03:00
|
|
|
# TODO: simplify?
|
2018-03-10 18:42:53 +03:00
|
|
|
filter_str = filter_str.lower()
|
|
|
|
number = self.get_active_number()
|
2018-05-15 13:40:59 +03:00
|
|
|
is_friend = self.is_active_a_friend()
|
2018-03-10 18:42:53 +03:00
|
|
|
if sorting > 1:
|
|
|
|
if sorting & 2:
|
|
|
|
self._contacts = sorted(self._contacts, key=lambda x: int(x.status is not None), reverse=True)
|
|
|
|
if sorting & 4:
|
|
|
|
if not sorting & 2:
|
|
|
|
self._contacts = sorted(self._contacts, key=lambda x: x.name.lower())
|
|
|
|
else: # save results of prev sorting
|
|
|
|
online_friends = filter(lambda x: x.status is not None, self._contacts)
|
|
|
|
count = len(list(online_friends))
|
|
|
|
part1 = self._contacts[:count]
|
|
|
|
part2 = self._contacts[count:]
|
|
|
|
part1 = sorted(part1, key=lambda x: x.name.lower())
|
|
|
|
part2 = sorted(part2, key=lambda x: x.name.lower())
|
|
|
|
self._contacts = part1 + part2
|
|
|
|
else: # sort by number
|
|
|
|
online_friends = filter(lambda x: x.status is not None, self._contacts)
|
|
|
|
count = len(list(online_friends))
|
|
|
|
part1 = self._contacts[:count]
|
|
|
|
part2 = self._contacts[count:]
|
|
|
|
part1 = sorted(part1, key=lambda x: x.number)
|
|
|
|
part2 = sorted(part2, key=lambda x: x.number)
|
|
|
|
self._contacts = part1 + part2
|
2018-05-01 21:40:29 +03:00
|
|
|
# self._screen.friends_list.clear()
|
|
|
|
# for contact in self._contacts:
|
|
|
|
# contact.set_widget(self.create_friend_item())
|
2018-03-10 18:42:53 +03:00
|
|
|
for index, friend in enumerate(self._contacts):
|
|
|
|
friend.visibility = (friend.status is not None or not (sorting & 1)) and (filter_str in friend.name.lower())
|
|
|
|
friend.visibility = friend.visibility or friend.messages or friend.actions
|
2018-05-01 21:40:29 +03:00
|
|
|
if friend.visibility:
|
|
|
|
self._screen.friends_list.item(index).setSizeHint(QtCore.QSize(250, self._friend_item_height))
|
|
|
|
else:
|
|
|
|
self._screen.friends_list.item(index).setSizeHint(QtCore.QSize(250, 0))
|
2018-03-10 18:42:53 +03:00
|
|
|
self._sorting, self._filter_string = sorting, filter_str
|
2018-04-18 23:55:51 +03:00
|
|
|
self._settings['sorting'] = self._sorting
|
|
|
|
self._settings.save()
|
2018-03-10 18:42:53 +03:00
|
|
|
self.set_active_by_number_and_type(number, is_friend)
|
|
|
|
|
|
|
|
def update_filtration(self):
|
|
|
|
"""
|
|
|
|
Update list of contacts when 1 of friends change connection status
|
|
|
|
"""
|
|
|
|
self.filtration_and_sorting(self._sorting, self._filter_string)
|
|
|
|
|
2018-04-18 23:55:51 +03:00
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
# Friend getters
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def get_friend_by_number(self, num):
|
|
|
|
return list(filter(lambda x: x.number == num and type(x) is Friend, self._contacts))[0]
|
|
|
|
|
|
|
|
def get_last_message(self):
|
2018-04-29 00:52:42 +03:00
|
|
|
if self._active_contact + 1:
|
2018-05-01 21:40:29 +03:00
|
|
|
return self.get_curr_contact().get_last_message_text()
|
2018-04-18 23:55:51 +03:00
|
|
|
else:
|
|
|
|
return ''
|
2018-03-10 18:42:53 +03:00
|
|
|
|
2018-04-18 23:55:51 +03:00
|
|
|
def get_active_number(self):
|
2018-04-30 22:28:33 +03:00
|
|
|
return self.get_curr_contact().number if self._active_contact + 1 else -1
|
2018-04-18 23:55:51 +03:00
|
|
|
|
|
|
|
def get_active_name(self):
|
2018-05-01 21:40:29 +03:00
|
|
|
return self.get_curr_contact().name if self._active_contact + 1 else ''
|
2018-04-18 23:55:51 +03:00
|
|
|
|
|
|
|
def is_active_online(self):
|
2018-05-01 21:40:29 +03:00
|
|
|
return self._active_contact + 1 and self.get_curr_contact().status is not None
|
2018-04-18 23:55:51 +03:00
|
|
|
|
|
|
|
def new_name(self, number, name):
|
2018-05-01 21:40:29 +03:00
|
|
|
# TODO: move to somewhere else?
|
2018-04-18 23:55:51 +03:00
|
|
|
friend = self.get_friend_by_number(number)
|
|
|
|
tmp = friend.name
|
|
|
|
friend.set_name(name)
|
|
|
|
name = str(name, 'utf-8')
|
|
|
|
if friend.name == name and tmp != name:
|
2018-05-05 00:09:33 +03:00
|
|
|
# TODO: move to friend?
|
2018-04-30 22:28:33 +03:00
|
|
|
message = util_ui.tr('User {} is now known as {}')
|
2018-05-05 00:09:33 +03:00
|
|
|
# message = message.format(tmp, name)
|
|
|
|
# friend.append_message(InfoMessage(0, message, util.get_unix_time()))
|
|
|
|
# friend.actions = True
|
|
|
|
# if number == self.get_active_number():
|
|
|
|
# self.create_message_item(message, time.time(), '', MESSAGE_TYPE['INFO_MESSAGE'])
|
|
|
|
# self._messages.scrollToBottom()
|
|
|
|
# self.set_active(None)
|
2018-03-10 18:42:53 +03:00
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
# Work with friends (remove, block, set alias, get public key)
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def set_alias(self, num):
|
|
|
|
"""
|
|
|
|
Set new alias for friend
|
|
|
|
"""
|
|
|
|
friend = self._contacts[num]
|
|
|
|
name = friend.name
|
2018-04-18 23:55:51 +03:00
|
|
|
text = util_ui.tr("Enter new alias for friend {} or leave empty to use friend's name:").format(name)
|
|
|
|
title = util_ui.tr('Set alias')
|
|
|
|
text, ok = util_ui.text_dialog(text, title, name)
|
2018-03-10 18:42:53 +03:00
|
|
|
if ok:
|
2018-04-18 23:55:51 +03:00
|
|
|
aliases = self._settings['friends_aliases']
|
2018-03-10 18:42:53 +03:00
|
|
|
if text:
|
|
|
|
friend.name = bytes(text, 'utf-8')
|
|
|
|
try:
|
|
|
|
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
|
|
|
|
friend.name = bytes(self._tox.friend_get_name(friend.number), 'utf-8')
|
|
|
|
friend.set_alias('')
|
|
|
|
try:
|
|
|
|
index = list(map(lambda x: x[0], aliases)).index(friend.tox_id)
|
|
|
|
del aliases[index]
|
|
|
|
except:
|
|
|
|
pass
|
2018-04-18 23:55:51 +03:00
|
|
|
self._settings.save()
|
2018-05-01 21:40:29 +03:00
|
|
|
# if num == self.get_active_number() and self.is_active_a_friend():
|
|
|
|
# self.update()
|
2018-03-10 18:42:53 +03:00
|
|
|
|
|
|
|
def friend_public_key(self, num):
|
|
|
|
return self._contacts[num].tox_id
|
|
|
|
|
2018-05-01 21:40:29 +03:00
|
|
|
def export_history(self, num, as_text):
|
|
|
|
contact = self._contacts[num]
|
|
|
|
return self._history.export_history(contact, as_text)
|
|
|
|
|
2018-03-10 18:42:53 +03:00
|
|
|
def delete_friend(self, num):
|
|
|
|
"""
|
|
|
|
Removes friend from contact list
|
|
|
|
:param num: number of friend in list
|
|
|
|
"""
|
|
|
|
friend = self._contacts[num]
|
|
|
|
try:
|
2018-04-18 23:55:51 +03:00
|
|
|
index = list(map(lambda x: x[0], self._settings['friends_aliases'])).index(friend.tox_id)
|
|
|
|
del self._settings['friends_aliases'][index]
|
2018-03-10 18:42:53 +03:00
|
|
|
except:
|
|
|
|
pass
|
2018-04-18 23:55:51 +03:00
|
|
|
if friend.tox_id in self._settings['notes']:
|
|
|
|
del self._settings['notes'][friend.tox_id]
|
|
|
|
self._settings.save()
|
2018-05-01 21:40:29 +03:00
|
|
|
self._history.delete_history(friend)
|
2018-03-10 18:42:53 +03:00
|
|
|
self._tox.friend_delete(friend.number)
|
|
|
|
del self._contacts[num]
|
|
|
|
self._screen.friends_list.takeItem(num)
|
2018-04-29 00:52:42 +03:00
|
|
|
if num == self._active_contact: # active friend was deleted
|
2018-05-01 21:40:29 +03:00
|
|
|
self.set_active(0 if len(self._contacts) else -1)
|
2018-03-10 18:42:53 +03:00
|
|
|
data = self._tox.get_savedata()
|
2018-04-18 23:55:51 +03:00
|
|
|
self._profile_manager.save_profile(data)
|
2018-03-10 18:42:53 +03:00
|
|
|
|
|
|
|
def add_friend(self, tox_id):
|
|
|
|
"""
|
|
|
|
Adds friend to list
|
|
|
|
"""
|
2018-04-30 22:28:33 +03:00
|
|
|
self._tox.friend_add_norequest(tox_id)
|
2018-05-15 22:51:42 +03:00
|
|
|
self._history.add_friend_to_db(tox_id)
|
2018-04-30 22:28:33 +03:00
|
|
|
friend = self._contact_provider.get_friend_by_public_key(tox_id)
|
2018-03-10 18:42:53 +03:00
|
|
|
self._contacts.append(friend)
|
2018-05-16 20:25:21 +03:00
|
|
|
friend.reset_avatar()
|
2018-03-10 18:42:53 +03:00
|
|
|
|
2018-05-19 00:07:49 +03:00
|
|
|
def add_group(self, group_number):
|
2018-05-19 16:00:28 +03:00
|
|
|
group = self._contact_provider.get_group_by_number(group_number)
|
2018-05-19 00:07:49 +03:00
|
|
|
self._contacts.append(group)
|
|
|
|
group.reset_avatar()
|
|
|
|
|
2018-03-10 18:42:53 +03:00
|
|
|
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]
|
2018-04-18 23:55:51 +03:00
|
|
|
if tox_id == self._tox.self_get_address[:TOX_PUBLIC_KEY_SIZE * 2]:
|
2018-03-10 18:42:53 +03:00
|
|
|
return
|
2018-04-18 23:55:51 +03:00
|
|
|
if tox_id not in self._settings['blocked']:
|
|
|
|
self._settings['blocked'].append(tox_id)
|
|
|
|
self._settings.save()
|
2018-03-10 18:42:53 +03:00
|
|
|
try:
|
|
|
|
num = self._tox.friend_by_public_key(tox_id)
|
|
|
|
self.delete_friend(num)
|
2018-04-18 23:55:51 +03:00
|
|
|
self.save_profile()
|
2018-03-10 18:42:53 +03:00
|
|
|
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
|
|
|
|
"""
|
2018-04-18 23:55:51 +03:00
|
|
|
self._settings['blocked'].remove(tox_id)
|
|
|
|
self._settings.save()
|
2018-03-10 18:42:53 +03:00
|
|
|
if add_to_friend_list:
|
|
|
|
self.add_friend(tox_id)
|
2018-04-18 23:55:51 +03:00
|
|
|
self.save_profile()
|
2018-03-10 18:42:53 +03:00
|
|
|
|
2018-05-11 00:35:56 +03:00
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
# Groups support
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def get_group_chats(self):
|
|
|
|
return list(filter(lambda c: type(c) is not Friend, self._contacts)) # TODO: fix after gc implementation
|
|
|
|
|
2018-03-10 18:42:53 +03:00
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
# Friend requests
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def send_friend_request(self, tox_id, message):
|
|
|
|
"""
|
|
|
|
Function tries to send request to contact with specified id
|
|
|
|
:param tox_id: id of new contact or tox dns 4 value
|
|
|
|
: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
|
2018-05-05 00:09:33 +03:00
|
|
|
tox_id = self._tox_dns.lookup(tox_id)
|
2018-03-10 18:42:53 +03:00
|
|
|
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)
|
2018-04-18 23:55:51 +03:00
|
|
|
title = util_ui.tr('Friend added')
|
|
|
|
text = util_ui.tr('Friend added without sending friend request')
|
|
|
|
util_ui.message_box(text, title)
|
2018-03-10 18:42:53 +03:00
|
|
|
else:
|
2018-05-01 21:40:29 +03:00
|
|
|
self._tox.friend_add(tox_id, message.encode('utf-8'))
|
2018-03-10 18:42:53 +03:00
|
|
|
tox_id = tox_id[:TOX_PUBLIC_KEY_SIZE * 2]
|
2018-05-01 21:40:29 +03:00
|
|
|
friend = self._contact_provider.get_friend_by_public_key(tox_id)
|
2018-03-10 18:42:53 +03:00
|
|
|
self._contacts.append(friend)
|
2018-04-18 23:55:51 +03:00
|
|
|
self.save_profile()
|
2018-03-10 18:42:53 +03:00
|
|
|
return True
|
|
|
|
except Exception as ex: # wrong data
|
2018-04-18 23:55:51 +03:00
|
|
|
util.log('Friend request failed with ' + str(ex))
|
2018-03-10 18:42:53 +03:00
|
|
|
return str(ex)
|
|
|
|
|
|
|
|
def process_friend_request(self, tox_id, message):
|
|
|
|
"""
|
|
|
|
Accept or ignore friend request
|
|
|
|
:param tox_id: tox id of contact
|
|
|
|
:param message: message
|
|
|
|
"""
|
2018-03-12 00:32:46 +03:00
|
|
|
if tox_id in self._settings['blocked']:
|
|
|
|
return
|
2018-03-10 18:42:53 +03:00
|
|
|
try:
|
2018-04-18 23:55:51 +03:00
|
|
|
text = util_ui.tr('User {} wants to add you to contact list. Message:\n{}')
|
|
|
|
reply = util_ui.question(text.format(tox_id, message), util_ui.tr('Friend request'))
|
|
|
|
if reply: # accepted
|
2018-03-10 18:42:53 +03:00
|
|
|
self.add_friend(tox_id)
|
|
|
|
data = self._tox.get_savedata()
|
2018-04-18 23:55:51 +03:00
|
|
|
self._profile_manager.save_profile(data)
|
2018-03-10 18:42:53 +03:00
|
|
|
except Exception as ex: # something is wrong
|
2018-04-18 23:55:51 +03:00
|
|
|
util.log('Accept friend request failed! ' + str(ex))
|
|
|
|
|
2018-05-01 16:39:09 +03:00
|
|
|
def can_send_typing_notification(self):
|
2018-05-01 21:40:29 +03:00
|
|
|
return self._settings['typing_notifications'] and self._active_contact + 1
|
2018-05-01 16:39:09 +03:00
|
|
|
|
2018-04-18 23:55:51 +03:00
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
2018-05-01 16:39:09 +03:00
|
|
|
# Private methods
|
2018-04-18 23:55:51 +03:00
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2018-05-01 16:39:09 +03:00
|
|
|
def _load_contacts(self):
|
|
|
|
self._load_friends()
|
|
|
|
self._load_groups()
|
|
|
|
if len(self._contacts):
|
|
|
|
self.set_active(0)
|
|
|
|
self.filtration_and_sorting(self._sorting)
|
|
|
|
|
|
|
|
def _load_friends(self):
|
|
|
|
self._contacts.extend(self._contact_provider.get_all_friends())
|
|
|
|
|
|
|
|
def _load_groups(self):
|
|
|
|
self._contacts.extend(self._contact_provider.get_all_groups())
|
2018-05-05 00:09:33 +03:00
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
# Current contact subscriptions
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def _subscribe_to_events(self, contact):
|
|
|
|
contact.name_changed_event.add_callback(self._current_contact_name_changed)
|
2018-05-10 20:47:34 +03:00
|
|
|
contact.status_changed_event.add_callback(self._current_contact_status_changed)
|
|
|
|
contact.status_message_changed_event.add_callback(self._current_contact_status_message_changed)
|
|
|
|
contact.avatar_changed_event.add_callback(self._current_contact_avatar_changed)
|
2018-05-05 00:09:33 +03:00
|
|
|
|
|
|
|
def _unsubscribe_from_events(self, contact):
|
|
|
|
contact.name_changed_event.remove_callback(self._current_contact_name_changed)
|
2018-05-10 20:47:34 +03:00
|
|
|
contact.status_changed_event.remove_callback(self._current_contact_status_changed)
|
|
|
|
contact.status_message_changed_event.remove_callback(self._current_contact_status_message_changed)
|
|
|
|
contact.avatar_changed_event.remove_callback(self._current_contact_avatar_changed)
|
2018-05-05 00:09:33 +03:00
|
|
|
|
|
|
|
def _current_contact_name_changed(self, name):
|
|
|
|
self._screen.account_name.setText(name)
|
2018-05-10 20:47:34 +03:00
|
|
|
|
|
|
|
def _current_contact_status_changed(self, status):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _current_contact_status_message_changed(self, status_message):
|
|
|
|
self._screen.account_status.setText(status_message)
|
|
|
|
|
|
|
|
def _current_contact_avatar_changed(self, avatar_path):
|
2018-05-16 19:04:02 +03:00
|
|
|
self._set_current_contact_avatar(avatar_path)
|
|
|
|
|
|
|
|
def _set_current_contact_data(self, contact):
|
|
|
|
self._screen.account_name.setText(contact.name)
|
|
|
|
self._screen.account_status.setText(contact.status_message)
|
|
|
|
self._set_current_contact_avatar(contact.get_avatar_path())
|
|
|
|
|
|
|
|
def _set_current_contact_avatar(self, avatar_path):
|
2018-05-10 20:47:34 +03:00
|
|
|
width = self._screen.account_avatar.width()
|
|
|
|
pixmap = QtGui.QPixmap(avatar_path)
|
2018-05-16 19:04:02 +03:00
|
|
|
self._screen.account_avatar.setPixmap(pixmap.scaled(width, width,
|
|
|
|
QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation))
|