toxygen/src/profile.py

212 lines
6.8 KiB
Python
Raw Normal View History

2016-02-26 19:54:15 +01:00
import mainscreen
2016-02-18 17:15:38 +01:00
from settings import Settings
2016-02-26 19:54:15 +01:00
from PySide import QtCore, QtGui
2016-02-18 17:15:38 +01:00
import os
2016-02-20 19:21:56 +01:00
from tox import Tox
from toxcore_enums_and_consts import *
from ctypes import *
2016-02-26 19:54:15 +01:00
from util import curr_time
2016-02-18 17:15:38 +01:00
2016-02-25 21:40:00 +01:00
class ProfileHelper(object):
2016-02-26 15:32:36 +01:00
"""
Class with static methods for search, load and save profiles
"""
2016-02-18 17:15:38 +01:00
@staticmethod
def find_profiles():
path = Settings.get_default_path()
result = []
# check default path
for fl in os.listdir(path):
if fl.endswith('.tox'):
2016-02-18 17:15:38 +01:00
name = fl[:-4]
result.append((path, name))
path = os.path.dirname(os.path.abspath(__file__))
# check current directory
for fl in os.listdir(path):
if fl.endswith('.tox'):
2016-02-18 17:15:38 +01:00
name = fl[:-4]
result.append((path, name))
return result
@staticmethod
def open_profile(path, name):
2016-02-25 21:40:00 +01:00
ProfileHelper._path = path + name + '.tox'
with open(ProfileHelper._path, 'rb') as fl:
data = fl.read()
if data:
2016-02-25 21:40:00 +01:00
print 'Data loaded from: {}'.format(ProfileHelper._path)
return data
else:
2016-02-25 21:40:00 +01:00
raise IOError('Save file not found. Path: {}'.format(ProfileHelper._path))
@staticmethod
2016-02-25 10:35:42 +01:00
def save_profile(data, name=None):
if name is not None:
2016-02-25 21:40:00 +01:00
ProfileHelper._path = Settings.get_default_path() + name + '.tox'
with open(ProfileHelper._path, 'wb') as fl:
fl.write(data)
2016-02-25 21:40:00 +01:00
print 'Data saved to: {}'.format(ProfileHelper._path)
class Contact(object):
2016-02-26 15:32:36 +01:00
"""
Class encapsulating TOX contact
Properties: name (alias of contact or name), status_message, status (connection status)
number - unique number of friend in list, widget - widget for update
"""
2016-02-25 21:40:00 +01:00
def __init__(self, name, status_message, number, widget):
2016-02-26 15:32:36 +01:00
# TODO: remove number
2016-02-25 21:40:00 +01:00
self._name, self._status_message, self._number = name, status_message, number
self._status, self._widget = None, widget
widget.name.setText(name)
widget.status_message.setText(status_message)
def getName(self):
return self._name
def setName(self, value):
self._name = value
self._widget.name.setText(value)
name = property(getName, setName)
def getStatusMessage(self):
return self._status_message
def setStatusMessage(self, value):
self._status_message = value
2016-02-26 19:54:15 +01:00
self._widget.status_message.setText(value)
2016-02-25 21:40:00 +01:00
status_message = property(getStatusMessage, setStatusMessage)
def getStatus(self):
return self._status
def setStatus(self, value):
2016-02-26 19:54:15 +01:00
# TODO: status repaint
2016-02-25 21:40:00 +01:00
self._status = value
status = property(getStatus, setStatus)
class Friend(Contact):
2016-02-26 15:32:36 +01:00
"""
Friend in list of friends. Can be hidden, unread messages added
"""
2016-02-25 21:40:00 +01:00
def __init__(self, *args):
super(Friend, self).__init__(*args)
self._new_messages = False
def setVisibility(self, value):
self._widget.setVisibility(value)
def setMessages(self, value):
self._new_messages = value
messages = property(None, setMessages)
def getNumber(self):
return self._number
number = property(getNumber)
2016-02-26 19:54:15 +01:00
# TODO: check if setNumber needed
2016-02-25 21:40:00 +01:00
2016-02-26 19:54:15 +01:00
class Profile(Contact):
2016-02-26 15:32:36 +01:00
"""
Profile of current toxygen user. Contains friends list, tox instance
"""
# TODO: add slices
2016-02-26 19:54:15 +01:00
def __init__(self, tox, widgets, widget, messages_list):
2016-02-25 21:40:00 +01:00
self._widget = widget
2016-02-26 19:54:15 +01:00
self._messages = messages_list
2016-02-25 21:40:00 +01:00
self.tox = tox
2016-02-26 19:54:15 +01:00
self._name = tox.self_get_name()
self._status_message = tox.self_get_status_message()
self._status = None
2016-02-25 21:40:00 +01:00
data = tox.self_get_friend_list()
self.friends, num, self._active_friend = [], 0, -1
for i in data:
2016-02-26 15:32:36 +01:00
name = tox.friend_get_name(i) or 'Tox user' # tox.friend_get_public_key(i)
2016-02-25 21:40:00 +01:00
status_message = tox.friend_get_status_message(i)
self.friends.append(Friend(name, status_message, i, widgets[num]))
num += 1
2016-02-26 19:54:15 +01:00
Profile._instance = self
@staticmethod
def getInstance():
return Profile._instance
2016-02-25 21:40:00 +01:00
def getActive(self):
return self._active_friend
def setActive(self, value):
if 0 <= value < self.tox.self_get_friend_list_size():
self._active_friend = value
active_friend = property(getActive, setActive)
def getActiveNumber(self):
return self.friends[self._active_friend].getNumber()
2016-02-26 19:54:15 +01:00
def getActiveName(self):
return self.friends[self._active_friend].getName()
2016-02-25 21:40:00 +01:00
def isActiveOnline(self):
if not self._active_friend + 1: # no active friend
return False
else:
# TODO: callbacks!
return True
status = self.friends[self._active_friend].getStatus()
return status is not None
2016-02-26 19:54:15 +01:00
def newMessage(self, id, message_type, message):
if id == self._active_friend: # add message to list
user_name = Profile.getInstance().getActiveName()
item = mainscreen.MessageItem(message, curr_time(), user_name)
elem = QtGui.QListWidgetItem(self._messages)
print item.sizeHint()
elem.setSizeHint(QtCore.QSize(250, 50))
self._messages.addItem(elem)
self._messages.setItemWidget(elem, item)
self._messages.repaint()
else:
friend = filter(lambda x: x.getNumber() == id, self.friends)[0]
friend.setMessages(True)
def changeStatus(self):
if self._status is not None:
self._status += 1
self._status %= 3
2016-02-20 19:21:56 +01:00
2016-02-23 22:03:50 +01:00
def tox_factory(data=None, settings=None):
2016-02-26 15:32:36 +01:00
"""
:param data: user data from .tox file. None = no saved data, create new profile
:param settings: current application settings. None = defaults settings will be used
:return: new tox instance
"""
2016-02-23 22:03:50 +01:00
if settings is None:
settings = Settings.get_default_settings()
2016-02-20 19:21:56 +01:00
tox_options = Tox.options_new()
tox_options.contents.udp_enabled = settings['udp_enabled']
tox_options.contents.proxy_type = settings['proxy_type']
tox_options.contents.proxy_host = settings['proxy_host']
tox_options.contents.proxy_port = settings['proxy_port']
tox_options.contents.start_port = settings['start_port']
tox_options.contents.end_port = settings['end_port']
tox_options.contents.tcp_port = settings['tcp_port']
2016-02-23 22:03:50 +01:00
if data: # load existing profile
tox_options.contents.savedata_type = TOX_SAVEDATA_TYPE['TOX_SAVE']
tox_options.contents.savedata_data = c_char_p(data)
tox_options.contents.savedata_length = len(data)
2016-02-25 12:22:15 +01:00
else: # create new profile
2016-02-23 22:03:50 +01:00
tox_options.contents.savedata_type = TOX_SAVEDATA_TYPE['NONE']
tox_options.contents.savedata_data = None
tox_options.contents.savedata_length = 0
2016-02-20 19:21:56 +01:00
return Tox(tox_options)