toxygen/toxygen/settings.py

294 lines
9.0 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-10-31 21:04:48 +00:00
from util import Singleton, curr_directory, log, copy, append_slash
2016-04-24 10:45:11 +00:00
import pyaudio
2017-02-11 17:07:28 +00:00
from toxes import ToxES
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()
2017-02-11 17:07:28 +00:00
inst = ToxES.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-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
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()
2016-09-21 18:14:53 +00:00
if os.path.isfile(p):
with open(p) as fl:
2016-03-02 16:05:03 +00:00
data = fl.read()
auto = json.loads(data)
2016-04-03 20:51:46 +00:00
if 'path' in auto and 'name' in auto:
path = str(auto['path'])
name = str(auto['name'])
if os.path.isfile(append_slash(path) + name + '.tox'):
return path, 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):
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-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():
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(path, name):
2017-02-20 18:33:04 +00:00
path = path + name + '.lock'
return os.path.isfile(path)
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,
2016-08-05 12:58:25 +00:00
'show_welcome_screen': True,
'close_to_tray': False,
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
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 {
'dark': '/styles/dark_style.qss',
'default': '/styles/style.qss'
2017-05-01 23:59:24 +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)
2017-02-11 17:07:28 +00:00
inst = ToxES.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):
2017-02-20 18:33:04 +00:00
profile_path = ProfileHelper.get_path()
path = str(profile_path + str(self.name) + '.lock')
2016-04-03 20:51:46 +00:00
if os.path.isfile(path):
2017-02-20 18:33:04 +00:00
os.remove(path)
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-10-27 21:55:34 +00:00
profile_path = ProfileHelper.get_path()
2017-02-20 18:33:04 +00:00
path = str(profile_path + str(self.name) + '.lock')
2016-04-03 20:51:46 +00:00
with open(path, 'w') as fl:
2017-02-20 18:33:04 +00:00
fl.write('active')
2016-04-03 20:51:46 +00:00
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-07-27 14:13:57 +00:00
def update_path(self):
self.path = ProfileHelper.get_path() + self.name + '.json'
2017-02-20 18:33:04 +00:00
@staticmethod
def get_global_settings_path():
return curr_directory() + '/toxygen.json'
2016-02-16 18:11:56 +00:00
@staticmethod
def get_default_path():
2016-07-26 16:09:57 +00:00
if system() == 'Windows':
2016-02-16 19:57:45 +00:00
return os.getenv('APPDATA') + '/Tox/'
2016-07-26 16:09:57 +00:00
elif system() == 'Darwin':
return os.getenv('HOME') + '/Library/Application Support/Tox/'
else:
return os.getenv('HOME') + '/.config/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-10-31 21:04:48 +00:00
path = append_slash(path)
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):
2017-02-11 17:07:28 +00:00
inst = ToxES.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
2016-07-27 14:13:57 +00:00
def export_profile(self, new_path, use_new_path):
path = new_path + os.path.basename(self._path)
2016-05-14 10:18:17 +00:00
with open(self._path, 'rb') as fin:
data = fin.read()
2016-07-27 14:13:57 +00:00
with open(path, 'wb') as fout:
2016-05-14 10:18:17 +00:00
fout.write(data)
2016-06-21 11:58:11 +00:00
print('Profile exported successfully')
2016-07-27 14:13:57 +00:00
copy(self._directory + 'avatars', new_path + 'avatars')
if use_new_path:
self._path = new_path + os.path.basename(self._path)
self._directory = new_path
Settings.get_instance().update_path()
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()