2016-02-16 20:03:37 +00:00
|
|
|
from platform import system
|
2016-02-16 18:11:56 +00:00
|
|
|
import json
|
2016-02-16 19:57:45 +00:00
|
|
|
import os
|
2016-04-05 16:39:05 +00:00
|
|
|
import locale
|
2016-05-13 09:08:45 +00:00
|
|
|
from util import Singleton, curr_directory, log
|
2016-04-24 10:45:11 +00:00
|
|
|
import pyaudio
|
2016-05-15 16:54:44 +00:00
|
|
|
from toxencryptsave import LibToxEncryptSave
|
2016-02-16 18:11:56 +00:00
|
|
|
|
|
|
|
|
2016-03-03 16:44:01 +00:00
|
|
|
class Settings(Singleton, dict):
|
2016-05-14 10:35:31 +00:00
|
|
|
"""
|
|
|
|
Settings of current profile + global app settings
|
|
|
|
"""
|
2016-02-16 18:11:56 +00:00
|
|
|
|
2016-04-25 12:48:56 +00:00
|
|
|
def __init__(self, name):
|
2016-04-03 20:51:46 +00:00
|
|
|
self.path = ProfileHelper.get_path() + str(name) + '.json'
|
2016-03-13 12:06:06 +00:00
|
|
|
self.name = name
|
2016-02-24 15:32:35 +00:00
|
|
|
if os.path.isfile(self.path):
|
2016-02-17 19:04:39 +00:00
|
|
|
with open(self.path) as fl:
|
|
|
|
data = fl.read()
|
2016-05-10 10:08:44 +00:00
|
|
|
try:
|
|
|
|
info = json.loads(data)
|
2016-05-13 09:08:45 +00:00
|
|
|
except Exception as ex:
|
2016-05-10 10:08:44 +00:00
|
|
|
info = Settings.get_default_settings()
|
2016-05-13 09:08:45 +00:00
|
|
|
log('Parsing settings error: ' + str(ex))
|
2016-05-10 10:08:44 +00:00
|
|
|
super(self.__class__, self).__init__(info)
|
2016-04-24 20:24:48 +00:00
|
|
|
self.upgrade()
|
2016-02-17 19:04:39 +00:00
|
|
|
else:
|
2016-02-17 21:16:44 +00:00
|
|
|
super(self.__class__, self).__init__(Settings.get_default_settings())
|
2016-02-17 20:47:43 +00:00
|
|
|
self.save()
|
2016-04-24 10:45:11 +00:00
|
|
|
p = pyaudio.PyAudio()
|
|
|
|
self.audio = {'input': p.get_default_input_device_info()['index'],
|
|
|
|
'output': p.get_default_output_device_info()['index']}
|
2016-02-17 19:04:39 +00:00
|
|
|
|
2016-03-02 16:05:03 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_auto_profile():
|
|
|
|
path = Settings.get_default_path() + 'toxygen.json'
|
|
|
|
if os.path.isfile(path):
|
|
|
|
with open(path) as fl:
|
|
|
|
data = fl.read()
|
|
|
|
auto = json.loads(data)
|
2016-04-03 20:51:46 +00:00
|
|
|
if 'path' in auto and 'name' in auto:
|
2016-04-05 16:39:05 +00:00
|
|
|
return unicode(auto['path']), unicode(auto['name'])
|
2016-03-02 16:05:03 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def set_auto_profile(path, name):
|
|
|
|
p = Settings.get_default_path() + 'toxygen.json'
|
2016-04-05 16:39:05 +00:00
|
|
|
data = json.dumps({'path': unicode(path.decode(locale.getpreferredencoding())), 'name': unicode(name)})
|
2016-03-02 16:05:03 +00:00
|
|
|
with open(p, 'w') as fl:
|
2016-04-24 20:24:48 +00:00
|
|
|
fl.write(data)
|
2016-03-02 16:05:03 +00:00
|
|
|
|
2016-02-17 21:16:44 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_default_settings():
|
2016-05-14 10:35:31 +00:00
|
|
|
"""
|
|
|
|
Default profile settings
|
|
|
|
"""
|
2016-02-17 21:16:44 +00:00
|
|
|
return {
|
2016-02-18 16:15:38 +00:00
|
|
|
'theme': 'default',
|
|
|
|
'ipv6_enabled': True,
|
|
|
|
'udp_enabled': True,
|
|
|
|
'proxy_type': 0,
|
|
|
|
'proxy_host': '0',
|
|
|
|
'proxy_port': 0,
|
|
|
|
'start_port': 0,
|
|
|
|
'end_port': 0,
|
|
|
|
'tcp_port': 0,
|
|
|
|
'notifications': True,
|
|
|
|
'sound_notifications': False,
|
2016-04-04 09:20:32 +00:00
|
|
|
'language': 'English',
|
2016-02-18 16:15:38 +00:00
|
|
|
'save_history': False,
|
|
|
|
'allow_inline': True,
|
2016-05-13 09:08:45 +00:00
|
|
|
'allow_auto_accept': True,
|
2016-03-18 16:33:54 +00:00
|
|
|
'auto_accept_path': None,
|
2016-02-22 21:18:58 +00:00
|
|
|
'show_online_friends': False,
|
2016-02-18 16:15:38 +00:00
|
|
|
'auto_accept_from_friends': [],
|
|
|
|
'friends_aliases': [],
|
2016-04-27 18:10:53 +00:00
|
|
|
'typing_notifications': False,
|
2016-04-24 20:24:48 +00:00
|
|
|
'calls_sound': True,
|
|
|
|
'blocked': []
|
2016-02-17 19:04:39 +00:00
|
|
|
}
|
2016-02-16 18:11:56 +00:00
|
|
|
|
2016-04-04 09:20:32 +00:00
|
|
|
@staticmethod
|
|
|
|
def supported_languages():
|
|
|
|
return [
|
|
|
|
('English', 'en_EN'),
|
2016-04-07 10:57:22 +00:00
|
|
|
('Russian', 'ru_RU'),
|
|
|
|
('French', 'fr_FR')
|
2016-04-04 09:20:32 +00:00
|
|
|
]
|
|
|
|
|
2016-04-24 20:24:48 +00:00
|
|
|
def upgrade(self):
|
|
|
|
default = Settings.get_default_settings()
|
|
|
|
for key in default:
|
|
|
|
if key not in self:
|
2016-04-25 12:48:56 +00:00
|
|
|
print key
|
2016-04-24 20:24:48 +00:00
|
|
|
self[key] = default[key]
|
|
|
|
self.save()
|
|
|
|
|
2016-02-16 19:57:45 +00:00
|
|
|
def save(self):
|
2016-02-17 21:16:44 +00:00
|
|
|
text = json.dumps(self)
|
2016-02-16 19:57:45 +00:00
|
|
|
with open(self.path, 'w') as fl:
|
|
|
|
fl.write(text)
|
|
|
|
|
2016-04-03 20:51:46 +00:00
|
|
|
def close(self):
|
|
|
|
path = Settings.get_default_path() + 'toxygen.json'
|
|
|
|
if os.path.isfile(path):
|
|
|
|
with open(path) as fl:
|
|
|
|
data = fl.read()
|
|
|
|
app_settings = json.loads(data)
|
2016-04-16 08:54:29 +00:00
|
|
|
try:
|
|
|
|
app_settings['active_profile'].remove(unicode(ProfileHelper.get_path() + self.name + '.tox'))
|
|
|
|
except:
|
|
|
|
pass
|
2016-04-03 20:51:46 +00:00
|
|
|
data = json.dumps(app_settings)
|
|
|
|
with open(path, 'w') as fl:
|
2016-04-04 21:12:02 +00:00
|
|
|
fl.write(data)
|
2016-04-03 20:51:46 +00:00
|
|
|
|
|
|
|
def set_active_profile(self):
|
2016-05-14 10:35:31 +00:00
|
|
|
"""
|
|
|
|
Mark current profile as active
|
|
|
|
"""
|
2016-04-03 20:51:46 +00:00
|
|
|
path = Settings.get_default_path() + 'toxygen.json'
|
|
|
|
if os.path.isfile(path):
|
|
|
|
with open(path) as fl:
|
|
|
|
data = fl.read()
|
|
|
|
app_settings = json.loads(data)
|
|
|
|
else:
|
|
|
|
app_settings = {}
|
|
|
|
if 'active_profile' not in app_settings:
|
|
|
|
app_settings['active_profile'] = []
|
2016-04-05 16:39:05 +00:00
|
|
|
profile_path = ProfileHelper.get_path()
|
|
|
|
app_settings['active_profile'].append(unicode(profile_path + str(self.name) + '.tox'))
|
2016-04-03 20:51:46 +00:00
|
|
|
data = json.dumps(app_settings)
|
|
|
|
with open(path, 'w') as fl:
|
|
|
|
fl.write(data)
|
|
|
|
|
2016-03-13 12:06:06 +00:00
|
|
|
def export(self, path):
|
|
|
|
text = json.dumps(self)
|
|
|
|
with open(path + str(self.name) + '.json', 'w') as fl:
|
|
|
|
fl.write(text)
|
|
|
|
|
2016-02-16 18:11:56 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_default_path():
|
2016-02-16 20:03:37 +00:00
|
|
|
if system() == 'Linux':
|
2016-02-16 19:57:45 +00:00
|
|
|
return os.getenv('HOME') + '/.config/tox/'
|
2016-02-16 20:03:37 +00:00
|
|
|
elif system() == 'Windows':
|
2016-02-16 19:57:45 +00:00
|
|
|
return os.getenv('APPDATA') + '/Tox/'
|
2016-04-03 20:51:46 +00:00
|
|
|
|
|
|
|
|
2016-05-14 10:18:17 +00:00
|
|
|
class ProfileHelper(Singleton):
|
2016-04-03 20:51:46 +00:00
|
|
|
"""
|
2016-05-14 10:18:17 +00:00
|
|
|
Class with methods for search, load and save profiles
|
2016-04-03 20:51:46 +00:00
|
|
|
"""
|
2016-05-14 10:18:17 +00:00
|
|
|
def __init__(self, path, name):
|
|
|
|
path = path.decode(locale.getpreferredencoding())
|
|
|
|
self._path = path + name + '.tox'
|
|
|
|
self._directory = path
|
|
|
|
# create /avatars if not exists:
|
|
|
|
directory = path + 'avatars'
|
|
|
|
if not os.path.exists(directory):
|
|
|
|
os.makedirs(directory)
|
|
|
|
|
|
|
|
def open_profile(self):
|
|
|
|
with open(self._path, 'rb') as fl:
|
|
|
|
data = fl.read()
|
|
|
|
if data:
|
|
|
|
return data
|
|
|
|
else:
|
|
|
|
raise IOError('Save file has zero size!')
|
|
|
|
|
|
|
|
def get_dir(self):
|
|
|
|
return self._directory
|
|
|
|
|
|
|
|
def save_profile(self, data):
|
2016-05-15 16:54:44 +00:00
|
|
|
inst = LibToxEncryptSave.get_instance()
|
|
|
|
if inst.has_password():
|
|
|
|
data = inst.pass_encrypt(data)
|
2016-05-14 10:18:17 +00:00
|
|
|
with open(self._path, 'wb') as fl:
|
|
|
|
fl.write(data)
|
|
|
|
print 'Profile saved successfully'
|
|
|
|
|
|
|
|
def export_profile(self, new_path):
|
|
|
|
new_path += os.path.basename(self._path)
|
|
|
|
with open(self._path, 'rb') as fin:
|
|
|
|
data = fin.read()
|
|
|
|
with open(new_path, 'wb') as fout:
|
|
|
|
fout.write(data)
|
|
|
|
print 'Profile exported successfully'
|
|
|
|
|
2016-04-03 20:51:46 +00:00
|
|
|
@staticmethod
|
|
|
|
def find_profiles():
|
2016-05-14 10:35:31 +00:00
|
|
|
"""
|
|
|
|
Find available tox profiles
|
|
|
|
"""
|
2016-04-03 20:51:46 +00:00
|
|
|
path = Settings.get_default_path()
|
|
|
|
result = []
|
|
|
|
# check default path
|
|
|
|
if not os.path.exists(path):
|
|
|
|
os.makedirs(path)
|
|
|
|
for fl in os.listdir(path):
|
|
|
|
if fl.endswith('.tox'):
|
|
|
|
name = fl[:-4]
|
|
|
|
result.append((path, name))
|
|
|
|
path = curr_directory()
|
|
|
|
# check current directory
|
|
|
|
for fl in os.listdir(path):
|
|
|
|
if fl.endswith('.tox'):
|
|
|
|
name = fl[:-4]
|
|
|
|
result.append((path + '/', name))
|
|
|
|
return result
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_active_profile(path, name):
|
2016-04-05 16:39:05 +00:00
|
|
|
path = path.decode(locale.getpreferredencoding()) + name + '.tox'
|
2016-04-03 20:51:46 +00:00
|
|
|
settings = Settings.get_default_path() + 'toxygen.json'
|
|
|
|
if os.path.isfile(settings):
|
|
|
|
with open(settings) as fl:
|
|
|
|
data = fl.read()
|
|
|
|
data = json.loads(data)
|
|
|
|
if 'active_profile' in data:
|
|
|
|
return path in data['active_profile']
|
2016-05-14 10:35:31 +00:00
|
|
|
return False
|
2016-04-03 20:51:46 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_path():
|
2016-05-14 10:18:17 +00:00
|
|
|
return ProfileHelper.get_instance().get_dir()
|