toxygen/toxygen/contacts/profile.py

88 lines
3.2 KiB
Python
Raw Normal View History

from contacts import basecontact
2018-05-15 19:51:42 +00:00
import random
2018-05-17 12:20:47 +00:00
import threading
2018-05-24 18:43:34 +00:00
import common.tox_save as tox_save
from middleware.threads import invoke_in_main_thread
2016-02-18 16:15:38 +00:00
2018-05-24 18:43:34 +00:00
class Profile(basecontact.BaseContact, tox_save.ToxSave):
2016-02-26 14:32:36 +00:00
"""
2018-07-16 18:29:15 +00:00
Profile of current toxygen user.
2016-02-26 14:32:36 +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,
profile_manager,
2016-10-22 18:23:03 +00:00
tox.self_get_name(),
tox.self_get_status_message(),
screen,
2016-10-22 18:23:03 +00:00
tox.self_get_address())
2018-05-24 18:43:34 +00:00
tox_save.ToxSave.__init__(self, tox)
2016-03-16 08:01:23 +00:00
self._screen = screen
2016-03-09 18:11:36 +00:00
self._messages = screen.messages
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-18 10:23:48 +00:00
self._timer = None
# -----------------------------------------------------------------------------------------------------------------
# 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-18 10:23:48 +00:00
self._timer = threading.Timer(50, self._reconnect)
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
2018-05-19 16:38:54 +00:00
super().set_name(value)
2018-07-21 17:43:16 +00:00
self._tox.self_set_name(self._name)
def set_status_message(self, value):
super().set_status_message(value)
2018-07-21 17:43:16 +00:00
self._tox.self_set_status_message(self._status_message)
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
2016-03-14 19:30:51 +00:00
# -----------------------------------------------------------------------------------------------------------------
# Reset
# -----------------------------------------------------------------------------------------------------------------
def restart(self):
2016-03-14 19:30:51 +00:00
"""
Recreate tox instance
"""
2016-03-15 17:05:19 +00:00
self.status = None
invoke_in_main_thread(self._reset_action)
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-24 18:43:34 +00:00
contacts = self._contacts_provider.get_all_friends()
all_friends_offline = all(list(map(lambda x: x.status is None, contacts)))
if self.status is None or (all_friends_offline and len(contacts)):
2017-03-03 19:09:45 +00:00
self._waiting_for_reconnection = True
self.restart()
2018-05-18 10:23:48 +00:00
self._timer = threading.Timer(50, self._reconnect)
2018-05-17 12:20:47 +00:00
self._timer.start()