BetterDiscordApp-v2/client/src/modules/settings.js

153 lines
5.1 KiB
JavaScript
Raw Normal View History

2018-01-30 11:01:24 +01:00
/**
* BetterDiscord Settings Module
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Utils, FileUtils, ClientLogger as Logger } from 'common';
2018-03-01 20:00:24 +01:00
import { SettingsSet, SettingUpdatedEvent } from 'structs';
2018-02-12 16:19:11 +01:00
import path from 'path';
import Globals from './globals';
import CssEditor from './csseditor';
import Events from './events';
import defaultSettings from '../data/user.settings.default';
2018-01-30 11:01:24 +01:00
2018-03-02 20:34:53 +01:00
export default new class Settings {
2018-03-02 20:34:53 +01:00
constructor() {
this.settings = defaultSettings.map(_set => {
const set = new SettingsSet(_set);
set.on('setting-updated', event => {
const { category, setting, value, old_value } = event;
Logger.log('Settings', [`${set.id}/${category.id}/${setting.id} was changed from`, old_value, 'to', value]);
Events.emit('setting-updated', event);
Events.emit(`setting-updated-${set.id}_${category.id}_${setting.id}`, event);
});
set.on('settings-updated', async (event) => {
await this.saveSettings();
Events.emit('settings-updated', event);
});
return set;
});
2018-03-02 20:34:53 +01:00
}
/**
* Loads BetterDiscord's settings.
*/
2018-03-02 20:34:53 +01:00
async loadSettings() {
2018-02-12 16:19:11 +01:00
try {
await FileUtils.ensureDirectory(this.dataPath);
const settingsPath = path.resolve(this.dataPath, 'user.settings.json');
const user_config = await FileUtils.readJsonFromFile(settingsPath);
2018-03-04 00:36:17 +01:00
const { settings, scss, css, css_editor_files, scss_error, css_editor_bounds } = user_config;
2018-02-12 16:19:11 +01:00
for (let set of this.settings) {
const newSet = settings.find(s => s.id === set.id);
if (!newSet) continue;
await set.merge(newSet);
set.setSaved();
}
2018-02-13 21:05:38 +01:00
2018-03-04 00:36:17 +01:00
CssEditor.setState(scss, css, css_editor_files, scss_error);
CssEditor.editor_bounds = css_editor_bounds || {};
2018-02-12 16:19:11 +01:00
} catch (err) {
// There was an error loading settings
// This probably means that the user doesn't have any settings yet
Logger.err('Settings', err);
}
}
/**
* Saves BetterDiscord's settings including CSS editor data.
*/
2018-03-02 20:34:53 +01:00
async saveSettings() {
2018-02-12 16:19:11 +01:00
try {
await FileUtils.ensureDirectory(this.dataPath);
const settingsPath = path.resolve(this.dataPath, 'user.settings.json');
await FileUtils.writeJsonToFile(settingsPath, {
2018-03-02 20:34:53 +01:00
settings: this.settings.map(set => set.strip()),
scss: CssEditor.scss,
2018-03-04 00:36:17 +01:00
css: CssEditor.css,
css_editor_files: CssEditor.files,
scss_error: CssEditor.error,
css_editor_bounds: CssEditor.editor_bounds
2018-02-12 16:19:11 +01:00
});
2018-03-01 20:00:24 +01:00
for (let set of this.settings) {
2018-03-01 20:00:24 +01:00
set.setSaved();
}
2018-02-12 16:19:11 +01:00
} catch (err) {
2018-03-01 20:00:24 +01:00
// There was an error saving settings
2018-02-12 16:19:11 +01:00
Logger.err('Settings', err);
2018-03-01 20:00:24 +01:00
throw err;
2018-02-12 16:19:11 +01:00
}
}
/**
* Finds one of BetterDiscord's settings sets.
* @param {String} set_id The ID of the set to find
* @return {SettingsSet}
*/
2018-03-02 20:34:53 +01:00
getSet(set_id) {
return this.settings.find(s => s.id === set_id);
}
2018-03-02 20:34:53 +01:00
get core() { return this.getSet('core') }
get ui() { return this.getSet('ui') }
get emotes() { return this.getSet('emotes') }
2018-03-04 00:36:17 +01:00
get css() { return this.getSet('css') }
2018-03-02 20:34:53 +01:00
get security() { return this.getSet('security') }
2018-03-01 20:00:24 +01:00
/**
* Finds a category in one of BetterDiscord's settings sets.
* @param {String} set_id The ID of the set to look in
* @param {String} category_id The ID of the category to find
* @return {SettingsCategory}
*/
2018-03-02 20:34:53 +01:00
getCategory(set_id, category_id) {
const set = this.getSet(set_id);
2018-03-01 20:00:24 +01:00
return set ? set.getCategory(category_id) : undefined;
}
/**
* Finds a setting in one of BetterDiscord's settings sets.
* @param {String} set_id The ID of the set to look in
* @param {String} category_id The ID of the category to look in
* @param {String} setting_id The ID of the setting to find
* @return {Setting}
*/
2018-03-02 20:34:53 +01:00
getSetting(set_id, category_id, setting_id) {
2018-03-01 20:00:24 +01:00
const set = this.getSet(set_id);
return set ? set.getSetting(category_id, setting_id) : undefined;
}
/**
* Returns a setting's value in one of BetterDiscord's settings sets.
* @param {String} set_id The ID of the set to look in
* @param {String} category_id The ID of the category to look in
* @param {String} setting_id The ID of the setting to find
* @return {Any}
*/
2018-03-02 20:34:53 +01:00
get(set_id, category_id, setting_id) {
2018-03-01 20:00:24 +01:00
const set = this.getSet(set_id);
return set ? set.get(category_id, setting_id) : undefined;
}
/**
* The path to store user data in.
*/
2018-03-02 20:34:53 +01:00
get dataPath() {
2018-03-19 17:45:20 +01:00
return Globals.getPath('data');
2018-01-30 11:01:24 +01:00
}
2018-01-30 11:01:24 +01:00
}