toxygen/toxygen/contacts/profile.py

111 lines
4.2 KiB
Python
Raw Normal View History

from contacts.friend import *
from file_transfers.file_transfers import *
2016-03-12 20:18:13 +00:00
import time
from contacts import basecontact
2018-05-10 17:47:34 +00:00
import utils.ui as util_ui
2018-05-15 19:51:42 +00:00
import random
2018-05-17 12:20:47 +00:00
import threading
2016-02-18 16:15:38 +00:00
class Profile(basecontact.BaseContact):
2016-02-26 14:32:36 +00:00
"""
2016-03-09 19:46:00 +00:00
Profile of current toxygen user. Contains friends list, tox instance
2016-02-26 14:32:36 +00:00
"""
2018-05-17 21:06:14 +00:00
def __init__(self, profile_manager, tox, screen, contacts_provider, reset_action):
"""
:param tox: tox instance
2016-03-09 18:11:36 +00:00
:param screen: ref to main screen
"""
2016-10-22 18:23:03 +00:00
basecontact.BaseContact.__init__(self,
2018-04-18 20:55:51 +00:00
profile_manager,
2016-10-22 18:23:03 +00:00
tox.self_get_name(),
tox.self_get_status_message(),
screen.user_info,
tox.self_get_address())
2016-03-16 08:01:23 +00:00
self._screen = screen
2016-03-09 18:11:36 +00:00
self._messages = screen.messages
2016-03-16 08:01:23 +00:00
self._tox = tox
2018-05-17 21:06:14 +00:00
self._contacts_provider = contacts_provider
self._reset_action = reset_action
2017-03-03 19:09:45 +00:00
self._waiting_for_reconnection = False
2018-05-17 21:06:14 +00:00
self._timer = threading.Timer(50, self._reconnect)
# -----------------------------------------------------------------------------------------------------------------
# Edit current user's data
# -----------------------------------------------------------------------------------------------------------------
def change_status(self):
2016-03-09 19:46:00 +00:00
"""
Changes status of user (online, away, busy)
"""
if self._status is not None:
2016-05-24 18:08:52 +00:00
self.set_status((self._status + 1) % 3)
def set_status(self, status):
super().set_status(status)
2016-06-25 12:40:01 +00:00
if status is not None:
2016-05-30 18:38:21 +00:00
self._tox.self_set_status(status)
2017-03-08 10:19:41 +00:00
elif not self._waiting_for_reconnection:
2017-04-22 19:35:32 +00:00
self._waiting_for_reconnection = True
2018-05-17 12:20:47 +00:00
self._timer.start()
def set_name(self, value):
2016-06-23 19:50:17 +00:00
if self.name == value:
return
2016-06-24 12:00:13 +00:00
tmp = self.name
super().set_name(value.encode('utf-8'))
2016-03-16 08:01:23 +00:00
self._tox.self_set_name(self._name.encode('utf-8'))
message = util_ui.tr('User {} is now known as {}')
2016-06-24 12:00:13 +00:00
message = message.format(tmp, value)
2016-10-22 18:23:03 +00:00
for friend in self._contacts:
2016-06-23 19:50:17 +00:00
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 19:50:17 +00:00
self._messages.scrollToBottom()
def set_status_message(self, value):
super().set_status_message(value)
2016-03-16 08:01:23 +00:00
self._tox.self_set_status_message(self._status_message.encode('utf-8'))
2018-05-17 21:06:14 +00:00
def set_new_nospam(self):
2016-06-08 15:35:40 +00:00
"""Sets new nospam part of tox id"""
2016-05-03 19:02:56 +00:00
self._tox.self_set_nospam(random.randint(0, 4294967295)) # no spam - uint32
self._tox_id = self._tox.self_get_address()
2016-05-03 19:02:56 +00:00
return self._tox_id
# -----------------------------------------------------------------------------------------------------------------
# Private messages
# -----------------------------------------------------------------------------------------------------------------
2016-06-15 21:12:27 +00: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-03-14 19:30:51 +00:00
# -----------------------------------------------------------------------------------------------------------------
# Reset
# -----------------------------------------------------------------------------------------------------------------
2018-05-17 21:06:14 +00:00
def _restart(self):
2016-03-14 19:30:51 +00:00
"""
Recreate tox instance
"""
2016-03-16 08:01:23 +00:00
del self._tox
2018-05-17 21:06:14 +00:00
self._tox = self._reset_action()
2016-03-15 17:05:19 +00:00
self.status = None
2016-03-14 19:30:51 +00:00
2018-05-17 21:06:14 +00:00
def _reconnect(self):
2017-03-03 19:09:45 +00:00
self._waiting_for_reconnection = False
2018-05-17 21:06:14 +00:00
contacts = self._contacts_provider.get_all()
if self.status is None or all(list(map(lambda x: x.status is None, contacts))) and len(contacts):
2017-03-03 19:09:45 +00:00
self._waiting_for_reconnection = True
2018-05-17 21:06:14 +00:00
self._restart()
2018-05-17 12:20:47 +00:00
self._timer.start()
2016-07-19 12:14:30 +00:00
2016-04-24 10:45:11 +00:00
def close(self):
2017-07-17 19:27:52 +00:00
for friend in filter(lambda x: type(x) is Friend, self._contacts):
2016-07-30 19:51:25 +00:00
self.friend_exit(friend.number)
2016-10-22 18:23:03 +00:00
for i in range(len(self._contacts)):
del self._contacts[0]