toxygen/toxygen/settings.py

270 lines
8.3 KiB
Python
Raw Normal View History

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-07-01 20:15:00 +00:00
from toxencryptsave import ToxEncryptSave
2016-06-11 10:36:52 +00:00
import smileys
2016-02-16 18:11:56 +00:00
2016-06-21 11:58:11 +00:00
class Settings(dict, Singleton):
2016-05-14 10:35:31 +00:00
"""
Settings of current profile + global app settings
"""
2016-02-16 18:11:56 +00:00
def __init__(self, name):
2016-06-22 11:35:22 +00:00
Singleton.__init__(self)
2016-06-21 11:58:11 +00:00
self.path = ProfileHelper.get_path() + str(name) + '.json'
2016-03-13 12:06:06 +00:00
self.name = name
if os.path.isfile(self.path):
2016-06-06 10:05:11 +00:00
with open(self.path, 'rb') as fl:
2016-02-17 19:04:39 +00:00
data = fl.read()
2016-07-01 20:15:00 +00:00
inst = ToxEncryptSave.get_instance()
try:
2016-06-05 11:59:36 +00:00
if inst.is_data_encrypted(data):
data = inst.pass_decrypt(data)
2016-06-21 11:58:11 +00:00
info = json.loads(str(data, 'utf-8'))
2016-05-13 09:08:45 +00:00
except Exception as ex:
info = Settings.get_default_settings()
2016-05-13 09:08:45 +00:00
log('Parsing settings error: ' + str(ex))
super(Settings, self).__init__(info)
self.upgrade()
2016-02-17 19:04:39 +00:00
else:
super(Settings, self).__init__(Settings.get_default_settings())
2016-02-17 20:47:43 +00:00
self.save()
2016-06-11 10:36:52 +00:00
smileys.SmileyLoader(self)
2016-04-24 10:45:11 +00:00
p = pyaudio.PyAudio()
2016-07-02 12:40:06 +00:00
self.locked = False
2016-04-24 10:45:11 +00:00
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-06-21 11:58:11 +00:00
return str(auto['path']), str(auto['name'])
2016-06-18 19:32:14 +00:00
return '', ''
2016-03-02 16:05:03 +00:00
@staticmethod
def set_auto_profile(path, name):
p = Settings.get_default_path() + 'toxygen.json'
2016-06-18 19:32:14 +00:00
with open(p) as fl:
data = fl.read()
data = json.loads(data)
2016-06-21 11:58:11 +00:00
data['path'] = str(path)
data['name'] = str(name)
2016-03-02 16:05:03 +00:00
with open(p, 'w') as fl:
2016-06-18 19:32:14 +00:00
fl.write(json.dumps(data))
@staticmethod
def reset_auto_profile():
p = Settings.get_default_path() + 'toxygen.json'
with open(p) as fl:
data = fl.read()
data = json.loads(data)
if 'path' in data:
del data['path']
del data['name']
with open(p, 'w') as fl:
fl.write(json.dumps(data))
@staticmethod
def is_active_profile(path, name):
2016-06-21 11:58:11 +00:00
path = path + name + '.tox'
2016-06-18 19:32:14 +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']
return False
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,
2016-07-02 19:19:04 +00:00
'proxy_host': '127.0.0.1',
'proxy_port': 9050,
2016-02-18 16:15:38 +00:00
'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,
'calls_sound': True,
2016-05-28 10:06:13 +00:00
'blocked': [],
2016-06-04 12:19:15 +00:00
'plugins': [],
2016-06-11 10:36:52 +00:00
'notes': {},
'smileys': True,
2016-06-11 12:37:52 +00:00
'smiley_pack': 'default',
'mirror_mode': False,
'width': 920,
'height': 500,
'x': 400,
'y': 400,
'message_font_size': 14,
'unread_color': 'red',
2016-07-01 13:25:46 +00:00
'save_unsent_only': False,
2016-07-04 20:30:45 +00:00
'compact_mode': False,
2016-07-11 19:47:39 +00:00
'show_welcome_screen': True,
'notify_all_gc': False
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():
2016-06-18 20:50:12 +00:00
return {
'English': 'en_EN',
'Russian': 'ru_RU',
'French': 'fr_FR'
}
2016-04-04 09:20:32 +00:00
def upgrade(self):
default = Settings.get_default_settings()
for key in default:
if key not in self:
2016-06-21 11:58:11 +00:00
print(key)
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-07-01 20:15:00 +00:00
inst = ToxEncryptSave.get_instance()
2016-05-15 20:02:05 +00:00
if inst.has_password():
text = bytes(inst.pass_encrypt(bytes(text, 'utf-8')))
else:
text = bytes(text, 'utf-8')
2016-06-06 10:05:11 +00:00
with open(self.path, 'wb') as fl:
2016-02-16 19:57:45 +00:00
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)
try:
2016-06-21 11:58:11 +00:00
app_settings['active_profile'].remove(str(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-07-06 13:25:04 +00:00
profilepath = ProfileHelper.get_path()
app_settings['active_profile'].append(str(profilepath + 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):
2016-06-22 11:35:22 +00:00
Singleton.__init__(self)
2016-05-14 10:18:17 +00:00
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-07-01 20:15:00 +00:00
inst = ToxEncryptSave.get_instance()
2016-05-15 16:54:44 +00:00
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)
2016-06-21 11:58:11 +00:00
print('Profile saved successfully')
2016-05-14 10:18:17 +00:00
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)
2016-06-21 11:58:11 +00:00
print('Profile exported successfully')
2016-05-14 10:18:17 +00:00
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 get_path():
2016-05-14 10:18:17 +00:00
return ProfileHelper.get_instance().get_dir()