2016-02-18 17:15:38 +01:00
|
|
|
from settings import Settings
|
|
|
|
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-18 17:15:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Profile(object):
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def find_profiles():
|
|
|
|
path = Settings.get_default_path()
|
|
|
|
result = []
|
|
|
|
# check default path
|
|
|
|
for fl in os.listdir(path):
|
2016-02-18 17:52:12 +01:00
|
|
|
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):
|
2016-02-18 17:52:12 +01:00
|
|
|
if fl.endswith('.tox'):
|
2016-02-18 17:15:38 +01:00
|
|
|
name = fl[:-4]
|
|
|
|
result.append((path, name))
|
|
|
|
return result
|
|
|
|
|
2016-02-18 17:52:12 +01:00
|
|
|
@staticmethod
|
|
|
|
def open_profile(path, name):
|
|
|
|
Profile._path = path + name + '.tox'
|
|
|
|
with open(Profile._path, 'rb') as fl:
|
|
|
|
data = fl.read()
|
|
|
|
if data:
|
|
|
|
print 'Data loaded from: {}'.format(Profile._path)
|
|
|
|
return data
|
|
|
|
else:
|
|
|
|
raise IOError('Save file not found. Path: {}'.format(Profile._path))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def save_profile(data):
|
2016-02-23 22:03:50 +01:00
|
|
|
if not hasattr(Profile, '_path'):
|
|
|
|
Profile._path = Settings.get_default_path()
|
2016-02-18 17:52:12 +01:00
|
|
|
with open(Profile._path, 'wb') as fl:
|
|
|
|
fl.write(data)
|
|
|
|
print 'Data saved to: {}'.format(Profile._path)
|
|
|
|
|
2016-02-20 19:21:56 +01:00
|
|
|
|
2016-02-23 22:03:50 +01:00
|
|
|
def tox_factory(data=None, settings=None):
|
|
|
|
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)
|
|
|
|
else: # create new profile
|
|
|
|
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)
|