joplin/ReactNativeClient/lib/models/setting.js

169 lines
4.2 KiB
JavaScript
Raw Normal View History

2017-06-24 18:06:28 +00:00
import { BaseModel } from 'lib/base-model.js';
import { Database } from 'lib/database.js';
2017-05-12 20:17:23 +00:00
class Setting extends BaseModel {
static tableName() {
return 'settings';
}
2017-07-03 19:50:45 +00:00
static modelType() {
return BaseModel.TYPE_SETTING;
2017-06-19 19:26:27 +00:00
}
2017-05-12 20:17:23 +00:00
static defaultSetting(key) {
if (!(key in this.defaults_)) throw new Error('Unknown key: ' + key);
let output = Object.assign({}, this.defaults_[key]);
output.key = key;
return output;
}
2017-05-16 21:46:21 +00:00
static keys() {
if (this.keys_) return this.keys_;
this.keys_ = [];
for (let n in this.defaults_) {
if (!this.defaults_.hasOwnProperty(n)) continue;
this.keys_.push(n);
}
return this.keys_;
}
2017-06-27 20:16:03 +00:00
static publicKeys() {
let output = [];
for (let n in this.defaults_) {
if (!this.defaults_.hasOwnProperty(n)) continue;
if (this.defaults_[n].public) output.push(n);
}
return output;
}
2017-05-12 20:17:23 +00:00
static load() {
2017-06-19 19:26:27 +00:00
this.cancelScheduleUpdate();
2017-05-12 20:17:23 +00:00
this.cache_ = [];
2017-06-19 19:26:27 +00:00
return this.modelSelectAll('SELECT * FROM settings').then((rows) => {
2017-06-11 21:11:14 +00:00
this.cache_ = rows;
2017-05-12 20:17:23 +00:00
});
}
2017-06-24 18:51:43 +00:00
static setConstant(key, value) {
2017-07-06 21:30:45 +00:00
if (!(key in this.constants_)) throw new Error('Unknown constant key: ' + key);
2017-06-24 18:51:43 +00:00
this.constants_[key] = value;
}
2017-05-12 20:17:23 +00:00
static setValue(key, value) {
2017-06-19 18:58:49 +00:00
if (!this.cache_) throw new Error('Settings have not been initialized!');
2017-05-12 20:17:23 +00:00
for (let i = 0; i < this.cache_.length; i++) {
if (this.cache_[i].key == key) {
2017-05-19 19:12:09 +00:00
if (this.cache_[i].value === value) return;
2017-05-12 20:17:23 +00:00
this.cache_[i].value = value;
2017-05-19 19:12:09 +00:00
this.scheduleUpdate();
2017-05-12 20:17:23 +00:00
return;
}
}
let s = this.defaultSetting(key);
s.value = value;
this.cache_.push(s);
2017-05-19 19:12:09 +00:00
this.scheduleUpdate();
2017-05-12 20:17:23 +00:00
}
static value(key) {
if (key in this.constants_) {
let output = this.constants_[key];
if (output == 'SET_ME') throw new Error('Setting constant has not been set: ' + key);
return output;
}
2017-06-24 18:51:43 +00:00
2017-06-19 18:58:49 +00:00
if (!this.cache_) throw new Error('Settings have not been initialized!');
2017-05-12 20:17:23 +00:00
for (let i = 0; i < this.cache_.length; i++) {
if (this.cache_[i].key == key) {
return this.cache_[i].value;
}
}
let s = this.defaultSetting(key);
return s.value;
}
2017-05-16 21:46:21 +00:00
// Currently only supports objects with properties one level deep
static object(key) {
let output = {};
let keys = this.keys();
for (let i = 0; i < keys.length; i++) {
let k = keys[i].split('.');
if (k[0] == key) {
output[k[1]] = this.value(keys[i]);
}
}
return output;
}
// Currently only supports objects with properties one level deep
static setObject(key, object) {
for (let n in object) {
if (!object.hasOwnProperty(n)) continue;
this.setValue(key + '.' + n, object[n]);
}
}
2017-05-19 19:12:09 +00:00
static saveAll() {
if (!this.updateTimeoutId_) return Promise.resolve();
2017-06-25 10:41:03 +00:00
this.logger().info('Saving settings...');
2017-05-19 19:12:09 +00:00
clearTimeout(this.updateTimeoutId_);
this.updateTimeoutId_ = null;
2017-06-11 21:11:14 +00:00
let queries = [];
queries.push('DELETE FROM settings');
for (let i = 0; i < this.cache_.length; i++) {
2017-06-27 20:58:27 +00:00
let s = Object.assign({}, this.cache_[i]);
delete s.public;
queries.push(Database.insertQuery(this.tableName(), s));
2017-06-11 21:11:14 +00:00
}
return BaseModel.db().transactionExecBatch(queries).then(() => {
2017-06-25 10:41:03 +00:00
this.logger().info('Settings have been saved.');
2017-05-19 19:12:09 +00:00
});
}
2017-05-12 20:17:23 +00:00
static scheduleUpdate() {
2017-05-19 19:12:09 +00:00
if (this.updateTimeoutId_) clearTimeout(this.updateTimeoutId_);
this.updateTimeoutId_ = setTimeout(() => {
this.saveAll();
2017-05-12 20:17:23 +00:00
}, 500);
}
2017-06-19 19:26:27 +00:00
static cancelScheduleUpdate() {
if (this.updateTimeoutId_) clearTimeout(this.updateTimeoutId_);
this.updateTimeoutId_ = null;
}
2017-05-12 20:17:23 +00:00
}
2017-06-06 20:01:43 +00:00
Setting.defaults_ = {
2017-06-27 20:16:03 +00:00
'activeFolderId': { value: '', type: 'string', public: false },
'sync.onedrive.auth': { value: '', type: 'string', public: false },
2017-07-16 16:06:05 +00:00
'sync.filesystem.path': { value: '', type: 'string', public: true },
2017-06-27 20:16:03 +00:00
'sync.target': { value: 'onedrive', type: 'string', public: true },
2017-07-18 19:57:49 +00:00
'sync.context': { value: '', type: 'string', public: false },
2017-07-04 19:12:30 +00:00
'editor': { value: '', type: 'string', public: true },
2017-07-18 18:04:47 +00:00
'locale': { value: 'en_GB', type: 'string', public: true },
2017-07-18 19:27:10 +00:00
'aliases': { value: '', type: 'string', public: true },
2017-06-06 20:01:43 +00:00
};
2017-06-24 18:51:43 +00:00
// Contains constants that are set by the application and
// cannot be modified by the user:
Setting.constants_ = {
2017-07-07 22:25:03 +00:00
'env': 'SET_ME',
2017-06-24 18:51:43 +00:00
'appName': 'joplin',
2017-06-29 20:52:52 +00:00
'appId': 'SET_ME', // Each app should set this identifier
'appType': 'SET_ME', // 'cli' or 'mobile'
2017-07-06 21:30:45 +00:00
'resourceDir': '',
'profileDir': '',
2017-07-10 20:03:46 +00:00
'tempDir': '',
2017-06-24 18:51:43 +00:00
}
2017-05-12 20:17:23 +00:00
export { Setting };