toxygen/toxygen/user_data/settings.py

232 lines
7.4 KiB
Python
Raw Normal View History

2016-02-16 18:11:56 +00:00
import json
2018-05-10 17:47:34 +00:00
from utils.util import *
2016-04-24 10:45:11 +00:00
import pyaudio
2016-02-16 18:11:56 +00:00
class Settings(dict):
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, toxes, path):
self._path = path
self._profile_path = path.replace('.json', '.tox')
self._toxes = toxes
if os.path.isfile(path):
with open(path, 'rb') as fl:
2016-02-17 19:04:39 +00:00
data = fl.read()
try:
if toxes.is_data_encrypted(data):
data = toxes.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().__init__(info)
2018-08-05 08:35:24 +00:00
self._upgrade()
2016-02-17 19:04:39 +00:00
else:
super().__init__(Settings.get_default_settings())
2018-08-05 08:35:24 +00:00
self.save()
2016-07-02 12:40:06 +00:00
self.locked = False
2016-08-05 12:58:25 +00:00
self.closing = False
2017-03-26 21:04:32 +00:00
self.unlockScreen = False
2016-07-19 12:14:30 +00:00
p = pyaudio.PyAudio()
input_devices = output_devices = 0
for i in range(p.get_device_count()):
device = p.get_device_info_by_index(i)
if device["maxInputChannels"]:
input_devices += 1
if device["maxOutputChannels"]:
output_devices += 1
self.audio = {'input': p.get_default_input_device_info()['index'] if input_devices else -1,
'output': p.get_default_output_device_info()['index'] if output_devices else -1,
'enabled': input_devices and output_devices}
2017-07-12 18:18:21 +00:00
self.video = {'device': -1, 'width': 640, 'height': 480, 'x': 0, 'y': 0}
2016-02-17 19:04:39 +00:00
2018-08-05 08:35:24 +00:00
# -----------------------------------------------------------------------------------------------------------------
# Public methods
# -----------------------------------------------------------------------------------------------------------------
def save(self):
text = json.dumps(self)
if self._toxes.has_password():
text = bytes(self._toxes.pass_encrypt(bytes(text, 'utf-8')))
else:
text = bytes(text, 'utf-8')
with open(self._path, 'wb') as fl:
fl.write(text)
def close(self):
path = self._profile_path + '.lock'
if os.path.isfile(path):
os.remove(path)
def set_active_profile(self):
"""
Mark current profile as active
"""
path = self._profile_path + '.lock'
with open(path, 'w') as fl:
fl.write('active')
def export(self, path):
text = json.dumps(self)
name = os.path.basename(self._path)
with open(join_path(path, str(name)), 'w') as fl:
fl.write(text)
def update_path(self, new_path):
self._path = new_path
self.save()
# -----------------------------------------------------------------------------------------------------------------
# Static methods
# -----------------------------------------------------------------------------------------------------------------
2016-03-02 16:05:03 +00:00
@staticmethod
def get_auto_profile():
2017-02-20 18:33:04 +00:00
p = Settings.get_global_settings_path()
2018-08-05 08:35:24 +00:00
if not os.path.isfile(p):
return None
with open(p) as fl:
data = fl.read()
try:
auto = json.loads(data)
except Exception as ex:
log(str(ex))
auto = {}
if 'profile_path' in auto:
path = str(auto['profile_path'])
if not os.path.isabs(path):
path = join_path(path, curr_directory(__file__))
if os.path.isfile(path):
return path
2016-03-02 16:05:03 +00:00
@staticmethod
def set_auto_profile(path):
2017-02-20 18:33:04 +00:00
p = Settings.get_global_settings_path()
2016-09-21 18:14:53 +00:00
if os.path.isfile(p):
with open(p) as fl:
data = fl.read()
data = json.loads(data)
else:
data = {}
data['profile_path'] = str(path)
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():
2017-02-20 18:33:04 +00:00
p = Settings.get_global_settings_path()
2016-09-21 18:14:53 +00:00
if os.path.isfile(p):
with open(p) as fl:
data = fl.read()
data = json.loads(data)
else:
data = {}
2016-06-18 19:32:14 +00:00
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(profile_path):
return os.path.isfile(profile_path + '.lock')
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 {
2017-05-01 23:59:24 +00:00
'theme': 'dark',
2018-02-05 20:32:33 +00:00
'ipv6_enabled': False,
2016-02-18 16:15:38 +00:00
'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-10-30 15:13:12 +00:00
'sorting': 0,
2016-02-18 16:15:38 +00:00
'auto_accept_from_friends': [],
'paused_file_transfers': {},
2016-07-30 18:43:28 +00:00
'resend_files': True,
2016-02-18 16:15:38 +00:00
'friends_aliases': [],
2016-07-13 20:09:34 +00:00
'show_avatars': False,
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,
2018-05-16 17:25:21 +00:00
'identicons': True,
2016-08-05 12:58:25 +00:00
'show_welcome_screen': True,
2018-08-04 14:46:02 +00:00
'close_app': 0,
2016-09-23 17:37:32 +00:00
'font': 'Times New Roman',
2017-07-18 18:36:14 +00:00
'update': 1,
'group_notifications': True,
'download_nodes_list': False,
2018-05-24 16:13:19 +00:00
'notify_all_gc': False,
'lan_discovery': True
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',
2017-04-30 21:44:55 +00:00
'French': 'fr_FR',
2016-06-18 20:50:12 +00:00
'Russian': 'ru_RU',
2017-04-30 21:44:55 +00:00
'Ukrainian': 'uk_UA'
2016-06-18 20:50:12 +00:00
}
2016-04-04 09:20:32 +00:00
2017-05-01 23:59:24 +00:00
@staticmethod
def built_in_themes():
return {
2018-08-04 14:46:02 +00:00
'dark': 'dark_style.qss',
'default': 'style.qss'
2017-05-01 23:59:24 +00:00
}
2017-02-20 18:33:04 +00:00
@staticmethod
def get_global_settings_path():
return os.path.join(get_base_directory(), 'toxygen.json')
2017-02-20 18:33:04 +00:00
2016-02-16 18:11:56 +00:00
@staticmethod
def get_default_path():
system = get_platform()
if system == 'Windows':
2016-02-16 19:57:45 +00:00
return os.getenv('APPDATA') + '/Tox/'
elif system == 'Darwin':
2016-07-26 16:09:57 +00:00
return os.getenv('HOME') + '/Library/Application Support/Tox/'
else:
return os.getenv('HOME') + '/.config/tox/'
2016-04-03 20:51:46 +00:00
2018-08-05 08:35:24 +00:00
# -----------------------------------------------------------------------------------------------------------------
# Private methods
# -----------------------------------------------------------------------------------------------------------------
def _upgrade(self):
default = Settings.get_default_settings()
for key in default:
if key not in self:
print(key)
self[key] = default[key]