toxygen/toxygen/contacts/profile.py

108 lines
4.0 KiB
Python
Raw Normal View History

2022-09-27 13:51:50 +00:00
# -*- mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*-
2022-11-20 01:11:51 +00:00
from contacts import basecontact
2022-09-27 12:38:39 +00:00
import random
import threading
import common.tox_save as tox_save
from middleware.threads import invoke_in_main_thread
2022-10-11 09:32:39 +00:00
iUMAXINT = 4294967295
2024-02-08 07:39:15 +00:00
iRECONNECT = 50
2022-10-11 09:32:39 +00:00
2022-09-27 13:51:50 +00:00
global LOG
import logging
LOG = logging.getLogger('app.'+__name__)
2022-09-27 12:38:39 +00:00
class Profile(basecontact.BaseContact, tox_save.ToxSave):
"""
Profile of current toxygen user.
"""
2024-02-08 07:39:15 +00:00
def __init__(self, profile_manager, tox, screen, contacts_provider, reset_action, app=None):
2022-09-27 12:38:39 +00:00
"""
:param tox: tox instance
:param screen: ref to main screen
"""
2022-09-27 13:51:50 +00:00
assert tox
2022-09-27 12:38:39 +00:00
basecontact.BaseContact.__init__(self,
profile_manager,
tox.self_get_name(),
tox.self_get_status_message(),
screen,
tox.self_get_address())
tox_save.ToxSave.__init__(self, tox)
self._screen = screen
self._messages = screen.messages
self._contacts_provider = contacts_provider
self._reset_action = reset_action
self._waiting_for_reconnection = False
self._timer = None
2024-02-08 07:39:15 +00:00
self._app = app
2022-09-27 12:38:39 +00:00
# Edit current user's data
2024-02-08 07:39:15 +00:00
def change_status(self) -> None:
2022-09-27 12:38:39 +00:00
"""
Changes status of user (online, away, busy)
"""
if self._status is not None:
self.set_status((self._status + 1) % 3)
2024-02-08 07:39:15 +00:00
def set_status(self, status) -> None:
2022-09-27 12:38:39 +00:00
super().set_status(status)
if status is not None:
self._tox.self_set_status(status)
elif not self._waiting_for_reconnection:
self._waiting_for_reconnection = True
2024-02-08 07:39:15 +00:00
self._timer = threading.Timer(iRECONNECT, self._reconnect)
2022-09-27 12:38:39 +00:00
self._timer.start()
2024-02-08 07:39:15 +00:00
def set_name(self, value) -> None:
2022-09-27 12:38:39 +00:00
if self.name == value:
return
super().set_name(value)
self._tox.self_set_name(self._name)
2024-02-08 07:39:15 +00:00
def set_status_message(self, value) -> None:
2022-09-27 12:38:39 +00:00
super().set_status_message(value)
self._tox.self_set_status_message(self._status_message)
def set_new_nospam(self):
"""Sets new nospam part of tox id"""
2022-10-11 09:32:39 +00:00
self._tox.self_set_nospam(random.randint(0, iUMAXINT)) # no spam - uint32
2022-09-27 12:38:39 +00:00
self._tox_id = self._tox.self_get_address()
2022-10-11 09:32:39 +00:00
self._sToxId = self._tox.self_get_address()
return self._sToxId
2022-09-27 12:38:39 +00:00
# Reset
2024-02-08 07:39:15 +00:00
def restart(self) -> None:
2022-09-27 12:38:39 +00:00
"""
Recreate tox instance
"""
self.status = None
invoke_in_main_thread(self._reset_action)
2024-02-08 07:39:15 +00:00
def _reconnect(self) -> None:
2022-09-27 12:38:39 +00:00
self._waiting_for_reconnection = False
2024-02-08 07:39:15 +00:00
if self._app and self._app.bAppExiting:
# dont do anything after the app has been shipped
# there's a segv that results
return
2022-09-27 12:38:39 +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)):
self._waiting_for_reconnection = True
self.restart()
2024-02-08 07:39:15 +00:00
self._timer = threading.Timer(iRECONNECT, self._reconnect)
2022-09-27 12:38:39 +00:00
self._timer.start()
2024-02-08 07:39:15 +00:00
# Current thread 0x00007901a13ccb80 (most recent call first):
2024-02-10 23:52:50 +00:00
# File "/usr/local/lib/python3.11/site-packages/toxygen_wrapper/tox.py", line 826 in self_get_friend_list_size
# File "/usr/local/lib/python3.11/site-packages/toxygen_wrapper/tox.py", line 838 in self_get_friend_list
2024-02-08 07:39:15 +00:00
# File "/mnt/o/var/local/src/toxygen/toxygen/contacts/contact_provider.py", line 45 in get_all_friends
# File "/mnt/o/var/local/src/toxygen/toxygen/contacts/profile.py", line 90 in _reconnect
# File "/usr/lib/python3.11/threading.py", line 1401 in run
# File "/usr/lib/python3.11/threading.py", line 1045 in _bootstrap_inner
# File "/usr/lib/python3.11/threading.py", line 1002 in _bootstrap
#