saving app settings to local file for desktop

This commit is contained in:
Antelle 2015-11-04 22:59:22 +03:00
parent 897a477855
commit 087a0a092c
2 changed files with 28 additions and 12 deletions

View File

@ -29,11 +29,15 @@ if (window.process && window.process.versions && window.process.versions.electro
filters: [{ name: 'KeePass files', extensions: ['kdbx'] }]
}, cb);
},
getUserDataPath: function(fileName) {
return this.req('path').join(this.remReq('app').getPath('userData'), fileName || '');
},
writeFile: function(path, data) {
this.req('fs').writeFileSync(path, new window.Buffer(data));
},
readFile: function(path) {
return new Uint8Array(this.req('fs').readFileSync(path));
readFile: function(path, encoding) {
var contents = this.req('fs').readFileSync(path, encoding);
return typeof contents === 'string' ? contents : new Uint8Array(contents);
},
fileExists: function(path) {
return this.req('fs').existsSync(path);

View File

@ -1,6 +1,9 @@
'use strict';
var Backbone = require('backbone');
var Backbone = require('backbone'),
Launcher = require('../comp/launcher');
var FileName = 'app-settings.json';
var AppSettingsModel = Backbone.Model.extend({
defaults: {
@ -13,18 +16,27 @@ var AppSettingsModel = Backbone.Model.extend({
},
load: function() {
if (typeof localStorage !== 'undefined' && localStorage.appSettings) {
try {
var data = JSON.parse(localStorage.appSettings);
this.set(data, { silent: true });
} catch (e) { /* failed to load settings */ }
}
try {
var data;
if (Launcher) {
data = JSON.parse(Launcher.readFile(Launcher.getUserDataPath(FileName), 'utf8'));
} else if (typeof localStorage !== 'undefined' && localStorage.appSettings) {
data = JSON.parse(localStorage.appSettings);
}
if (data) {
this.set(data, {silent: true});
}
} catch (e) { /* TODO: log failed to load settings */ }
},
save: function() {
if (typeof localStorage !== 'undefined') {
localStorage.appSettings = JSON.stringify(this.attributes);
}
try {
if (Launcher) {
Launcher.writeFile(Launcher.getUserDataPath(FileName), JSON.stringify(this.attributes));
} else if (typeof localStorage !== 'undefined') {
localStorage.appSettings = JSON.stringify(this.attributes);
}
} catch (e) { /* TODO: log failed to save settings */ }
}
});