2016-02-24 09:33:49 +01:00
|
|
|
from PySide import QtCore
|
2016-02-24 19:38:36 +01:00
|
|
|
from notifications import *
|
2016-02-24 21:01:25 +01:00
|
|
|
from settings import Settings
|
2016-02-26 19:54:15 +01:00
|
|
|
from profile import Profile
|
2016-02-28 22:33:35 +01:00
|
|
|
from toxcore_enums_and_consts import *
|
2016-03-09 19:11:36 +01:00
|
|
|
from tox import bin_to_string
|
2016-02-22 16:55:04 +01:00
|
|
|
|
|
|
|
|
2016-02-24 09:33:49 +01:00
|
|
|
class InvokeEvent(QtCore.QEvent):
|
|
|
|
EVENT_TYPE = QtCore.QEvent.Type(QtCore.QEvent.registerEventType())
|
|
|
|
|
|
|
|
def __init__(self, fn, *args, **kwargs):
|
|
|
|
QtCore.QEvent.__init__(self, InvokeEvent.EVENT_TYPE)
|
|
|
|
self.fn = fn
|
|
|
|
self.args = args
|
|
|
|
self.kwargs = kwargs
|
|
|
|
|
|
|
|
|
|
|
|
class Invoker(QtCore.QObject):
|
|
|
|
|
|
|
|
def event(self, event):
|
|
|
|
event.fn(*event.args, **event.kwargs)
|
|
|
|
return True
|
|
|
|
|
|
|
|
_invoker = Invoker()
|
|
|
|
|
|
|
|
|
|
|
|
def invoke_in_main_thread(fn, *args, **kwargs):
|
|
|
|
QtCore.QCoreApplication.postEvent(_invoker, InvokeEvent(fn, *args, **kwargs))
|
|
|
|
|
|
|
|
|
2016-02-29 17:39:43 +01:00
|
|
|
def self_connection_status(tox_link):
|
2016-02-24 21:01:25 +01:00
|
|
|
"""
|
|
|
|
:param tox_link: tox instance
|
|
|
|
:return: function for tox.callback_self_connection_status
|
|
|
|
"""
|
2016-02-24 09:33:49 +01:00
|
|
|
def wrapped(tox, connection, user_data):
|
|
|
|
print 'Connection status: ', str(connection)
|
2016-02-29 16:40:49 +01:00
|
|
|
profile = Profile.get_instance()
|
|
|
|
if profile.status is None:
|
|
|
|
status = tox_link.self_get_status()
|
|
|
|
invoke_in_main_thread(profile.set_status, status)
|
|
|
|
elif connection == TOX_CONNECTION['NONE']:
|
|
|
|
invoke_in_main_thread(profile.set_status, None)
|
2016-02-23 13:07:15 +01:00
|
|
|
return wrapped
|
2016-02-22 16:55:04 +01:00
|
|
|
|
|
|
|
|
2016-02-28 22:33:35 +01:00
|
|
|
def friend_status(tox, friend_num, new_status, user_data):
|
|
|
|
"""
|
|
|
|
Check friend's status (none, busy, away)
|
|
|
|
"""
|
2016-03-04 18:52:52 +01:00
|
|
|
print "Friend's #{} status changed! New status: {}".format(friend_num, new_status)
|
2016-02-28 22:33:35 +01:00
|
|
|
profile = Profile.get_instance()
|
2016-02-29 16:40:49 +01:00
|
|
|
friend = profile.get_friend_by_number(friend_num)
|
2016-02-28 22:33:35 +01:00
|
|
|
invoke_in_main_thread(friend.set_status, new_status)
|
|
|
|
|
|
|
|
|
|
|
|
def friend_connection_status(tox, friend_num, new_status, user_data):
|
|
|
|
"""
|
|
|
|
Check friend's connection status (offline, udp, tcp)
|
|
|
|
"""
|
2016-03-04 18:52:52 +01:00
|
|
|
print "Friend #{} connected! Friend's status: {}".format(friend_num, new_status)
|
2016-02-28 22:33:35 +01:00
|
|
|
profile = Profile.get_instance()
|
2016-02-29 16:40:49 +01:00
|
|
|
friend = profile.get_friend_by_number(friend_num)
|
2016-02-28 22:33:35 +01:00
|
|
|
if new_status == TOX_CONNECTION['NONE']:
|
|
|
|
invoke_in_main_thread(friend.set_status, None)
|
2016-03-07 19:00:00 +01:00
|
|
|
invoke_in_main_thread(profile.update_filtration)
|
2016-02-28 22:33:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
def friend_name(window):
|
|
|
|
"""
|
|
|
|
:param window: main window
|
|
|
|
:return: function for callback friend_name. It updates friend's name
|
|
|
|
and calls window repaint
|
|
|
|
"""
|
|
|
|
def wrapped(tox, friend_num, name, size, user_data):
|
|
|
|
profile = Profile.get_instance()
|
2016-02-29 16:40:49 +01:00
|
|
|
friend = profile.get_friend_by_number(friend_num)
|
2016-02-28 22:33:35 +01:00
|
|
|
print 'New name: ', str(friend_num), str(name)
|
|
|
|
invoke_in_main_thread(friend.set_name, name)
|
2016-02-29 17:39:43 +01:00
|
|
|
if profile.get_active_number() == friend_num:
|
2016-03-09 19:11:36 +01:00
|
|
|
invoke_in_main_thread(profile.set_active)
|
2016-02-28 22:33:35 +01:00
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
|
|
|
def friend_status_message(window):
|
|
|
|
"""
|
|
|
|
:param window: main window
|
|
|
|
:return: function for callback friend_status_message. It updates friend's status message
|
|
|
|
and calls window repaint
|
|
|
|
"""
|
|
|
|
def wrapped(tox, friend_num, status_message, size, user_data):
|
|
|
|
profile = Profile.get_instance()
|
2016-02-29 16:40:49 +01:00
|
|
|
friend = profile.get_friend_by_number(friend_num)
|
2016-02-28 22:33:35 +01:00
|
|
|
invoke_in_main_thread(friend.set_status_message, status_message)
|
|
|
|
print 'User #{} has new status: {}'.format(friend_num, status_message)
|
2016-02-29 17:39:43 +01:00
|
|
|
if profile.get_active_number() == friend_num:
|
2016-03-09 19:11:36 +01:00
|
|
|
invoke_in_main_thread(profile.set_active)
|
2016-02-28 22:33:35 +01:00
|
|
|
return wrapped
|
2016-02-22 16:55:04 +01:00
|
|
|
|
|
|
|
|
2016-02-24 21:01:25 +01:00
|
|
|
def friend_message(window):
|
|
|
|
"""
|
|
|
|
:param window: main window
|
2016-02-28 22:33:35 +01:00
|
|
|
:return: function for tox.callback_friend_message. Adds new message to list
|
2016-02-24 21:01:25 +01:00
|
|
|
"""
|
|
|
|
def wrapped(tox, friend_number, message_type, message, size, user_data):
|
|
|
|
print 'Message: ', message.decode('utf8')
|
2016-02-28 22:33:35 +01:00
|
|
|
profile = Profile.get_instance()
|
2016-03-11 12:37:45 +01:00
|
|
|
settings = Settings.get_instance()
|
2016-02-28 22:33:35 +01:00
|
|
|
invoke_in_main_thread(profile.new_message, friend_number, message_type, message)
|
2016-03-11 12:37:45 +01:00
|
|
|
if not window.isActiveWindow():
|
|
|
|
friend = profile.get_friend_by_number(friend_number)
|
|
|
|
if settings['notifications']:
|
|
|
|
invoke_in_main_thread(tray_notification, friend.name, message.decode('utf8'))
|
2016-02-24 21:01:25 +01:00
|
|
|
return wrapped
|
2016-02-22 22:18:58 +01:00
|
|
|
|
|
|
|
|
2016-03-09 19:11:36 +01:00
|
|
|
def friend_request(tox, public_key, message, message_size, user_data):
|
2016-03-09 19:45:38 +01:00
|
|
|
"""
|
|
|
|
Called when user get new friend request
|
|
|
|
"""
|
2016-03-09 19:11:36 +01:00
|
|
|
profile = Profile.get_instance()
|
|
|
|
tox_id = bin_to_string(public_key, TOX_PUBLIC_KEY_SIZE)
|
|
|
|
invoke_in_main_thread(profile.process_friend_request, tox_id, message.decode('utf-8'))
|
|
|
|
|
|
|
|
|
2016-02-23 12:11:00 +01:00
|
|
|
def init_callbacks(tox, window):
|
|
|
|
"""
|
2016-02-28 22:33:35 +01:00
|
|
|
Initialization of all callbacks.
|
2016-02-23 12:11:00 +01:00
|
|
|
:param tox: tox instance
|
|
|
|
:param window: main window
|
|
|
|
"""
|
2016-02-22 22:18:58 +01:00
|
|
|
tox.callback_friend_status(friend_status, 0)
|
2016-02-24 21:01:25 +01:00
|
|
|
tox.callback_friend_message(friend_message(window), 0)
|
2016-02-29 17:39:43 +01:00
|
|
|
tox.callback_self_connection_status(self_connection_status(tox), 0)
|
2016-02-28 22:33:35 +01:00
|
|
|
tox.callback_friend_connection_status(friend_connection_status, 0)
|
|
|
|
tox.callback_friend_name(friend_name(window), 0)
|
|
|
|
tox.callback_friend_status_message(friend_status_message(window), 0)
|
2016-03-09 19:11:36 +01:00
|
|
|
tox.callback_friend_request(friend_request, 0)
|