2015-10-17 23:49:24 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-18 16:02:00 +02:00
|
|
|
var Backbone = require('backbone'),
|
|
|
|
FeatureDetector = require('../../util/feature-detector'),
|
2015-10-25 10:44:19 +01:00
|
|
|
PasswordGenerator = require('../../util/password-generator'),
|
|
|
|
Alerts = require('../../comp/alerts'),
|
|
|
|
Launcher = require('../../comp/launcher'),
|
2015-12-02 21:39:40 +01:00
|
|
|
Storage = require('../../storage'),
|
2015-10-23 23:21:21 +02:00
|
|
|
Links = require('../../const/links'),
|
2015-10-25 17:27:34 +01:00
|
|
|
DropboxLink = require('../../comp/dropbox-link'),
|
2015-12-12 09:53:50 +01:00
|
|
|
Format = require('../../util/format'),
|
2015-12-17 19:25:25 +01:00
|
|
|
Locale = require('../../util/locale'),
|
2015-10-24 18:43:30 +02:00
|
|
|
kdbxweb = require('kdbxweb'),
|
2015-10-18 16:02:00 +02:00
|
|
|
FileSaver = require('filesaver');
|
2015-10-17 23:49:24 +02:00
|
|
|
|
2016-03-14 06:04:55 +01:00
|
|
|
var SettingsFileView = Backbone.View.extend({
|
2015-12-16 22:50:45 +01:00
|
|
|
template: require('templates/settings/settings-file.hbs'),
|
2015-10-17 23:49:24 +02:00
|
|
|
|
2015-10-18 16:02:00 +02:00
|
|
|
events: {
|
2015-12-08 20:18:35 +01:00
|
|
|
'click .settings__file-button-save-default': 'saveDefault',
|
2015-10-18 16:02:00 +02:00
|
|
|
'click .settings__file-button-save-file': 'saveToFile',
|
|
|
|
'click .settings__file-button-export-xml': 'exportAsXml',
|
2015-12-08 22:00:31 +01:00
|
|
|
'click .settings__file-button-save-dropbox': 'saveToDropbox',
|
2015-11-07 21:37:54 +01:00
|
|
|
'click .settings__file-button-close': 'closeFile',
|
2015-10-24 18:43:30 +02:00
|
|
|
'change #settings__file-key-file': 'keyFileChange',
|
2015-12-15 18:42:44 +01:00
|
|
|
'click #settings__file-file-select-link': 'triggerSelectFile',
|
2015-10-24 18:43:30 +02:00
|
|
|
'change #settings__file-file-select': 'fileSelected',
|
2015-10-22 22:51:40 +02:00
|
|
|
'focus #settings__file-master-pass': 'focusMasterPass',
|
2016-03-04 17:39:29 +01:00
|
|
|
'input #settings__file-master-pass': 'changeMasterPass',
|
2015-10-24 18:43:30 +02:00
|
|
|
'blur #settings__file-master-pass': 'blurMasterPass',
|
2016-03-04 17:39:29 +01:00
|
|
|
'input #settings__file-name': 'changeName',
|
|
|
|
'input #settings__file-def-user': 'changeDefUser',
|
2015-10-24 18:43:30 +02:00
|
|
|
'change #settings__file-trash': 'changeTrash',
|
2016-03-04 17:39:29 +01:00
|
|
|
'input #settings__file-hist-len': 'changeHistoryLength',
|
|
|
|
'input #settings__file-hist-size': 'changeHistorySize',
|
|
|
|
'input #settings__file-key-rounds': 'changeKeyRounds'
|
2015-10-18 16:02:00 +02:00
|
|
|
},
|
|
|
|
|
2015-12-08 20:18:35 +01:00
|
|
|
appModel: null,
|
|
|
|
|
2015-10-23 22:12:12 +02:00
|
|
|
initialize: function() {
|
2016-03-04 17:39:29 +01:00
|
|
|
this.listenTo(this.model, 'change:syncing change:syncError change:syncDate', this.deferRender);
|
2015-10-23 22:12:12 +02:00
|
|
|
},
|
|
|
|
|
2015-10-17 23:49:24 +02:00
|
|
|
render: function() {
|
2015-10-18 16:02:00 +02:00
|
|
|
this.renderTemplate({
|
2015-10-22 22:39:30 +02:00
|
|
|
cmd: FeatureDetector.actionShortcutSymbol(true),
|
2015-10-25 17:27:34 +01:00
|
|
|
supportFiles: !!Launcher,
|
2015-10-23 23:21:21 +02:00
|
|
|
desktopLink: Links.Desktop,
|
2015-10-23 22:12:12 +02:00
|
|
|
|
2015-10-22 22:39:30 +02:00
|
|
|
name: this.model.get('name'),
|
2015-10-23 22:12:12 +02:00
|
|
|
path: this.model.get('path'),
|
2015-10-25 17:27:34 +01:00
|
|
|
storage: this.model.get('storage'),
|
2015-10-26 22:07:43 +01:00
|
|
|
syncing: this.model.get('syncing'),
|
2015-12-12 09:53:50 +01:00
|
|
|
syncError: this.model.get('syncError'),
|
|
|
|
syncDate: Format.dtStr(this.model.get('syncDate')),
|
2015-10-25 10:44:19 +01:00
|
|
|
password: PasswordGenerator.present(this.model.get('passwordLength')),
|
2015-10-22 22:39:30 +02:00
|
|
|
defaultUser: this.model.get('defaultUser'),
|
|
|
|
recycleBinEnabled: this.model.get('recycleBinEnabled'),
|
|
|
|
historyMaxItems: this.model.get('historyMaxItems'),
|
|
|
|
historyMaxSize: Math.round(this.model.get('historyMaxSize') / 1024 / 1024),
|
|
|
|
keyEncryptionRounds: this.model.get('keyEncryptionRounds')
|
2015-10-18 16:02:00 +02:00
|
|
|
});
|
2015-10-24 18:43:30 +02:00
|
|
|
if (!this.model.get('created')) {
|
|
|
|
this.$el.find('.settings__file-master-pass-warning').toggle(this.model.get('passwordChanged'));
|
|
|
|
}
|
|
|
|
this.renderKeyFileSelect();
|
|
|
|
},
|
|
|
|
|
|
|
|
renderKeyFileSelect: function() {
|
|
|
|
var keyFileName = this.model.get('keyFileName'),
|
|
|
|
oldKeyFileName = this.model.get('oldKeyFileName'),
|
|
|
|
keyFileChanged = this.model.get('keyFileChanged');
|
|
|
|
var sel = this.$el.find('#settings__file-key-file');
|
|
|
|
sel.html('');
|
|
|
|
if (keyFileName && keyFileChanged) {
|
2015-12-17 19:25:25 +01:00
|
|
|
var text = keyFileName !== 'Generated' ? Locale.setFileUseKeyFile + ' ' + keyFileName : Locale.setFileUseGenKeyFile;
|
2015-10-24 18:43:30 +02:00
|
|
|
$('<option/>').val('ex').text(text).appendTo(sel);
|
|
|
|
}
|
|
|
|
if (oldKeyFileName) {
|
2015-12-17 19:25:25 +01:00
|
|
|
var useText = keyFileChanged ? Locale.setFileUseOldKeyFile : Locale.setFileUseKeyFile + ' ' + oldKeyFileName;
|
|
|
|
$('<option/>').val('old').text(useText).appendTo(sel);
|
2015-10-24 18:43:30 +02:00
|
|
|
}
|
2015-12-17 19:25:25 +01:00
|
|
|
$('<option/>').val('gen').text(Locale.setFileGenKeyFile).appendTo(sel);
|
|
|
|
$('<option/>').val('none').text(Locale.setFileDontUseKeyFile).appendTo(sel);
|
2015-10-24 18:43:30 +02:00
|
|
|
if (keyFileName && keyFileChanged) {
|
|
|
|
sel.val('ex');
|
|
|
|
} else if (!keyFileName) {
|
|
|
|
sel.val('none');
|
|
|
|
} else if (oldKeyFileName && keyFileName === oldKeyFileName && !keyFileChanged) {
|
|
|
|
sel.val('old');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-12-08 20:18:35 +01:00
|
|
|
validatePassword: function(continueCallback) {
|
2015-10-24 18:43:30 +02:00
|
|
|
if (!this.model.get('passwordLength')) {
|
2015-12-08 19:02:50 +01:00
|
|
|
var that = this;
|
|
|
|
Alerts.yesno({
|
2015-12-17 19:25:25 +01:00
|
|
|
header: Locale.setFileEmptyPass,
|
|
|
|
body: Locale.setFileEmptyPassBody,
|
2015-12-08 19:02:50 +01:00
|
|
|
success: function() {
|
2015-12-08 20:18:35 +01:00
|
|
|
continueCallback();
|
2015-12-08 19:02:50 +01:00
|
|
|
},
|
|
|
|
cancel: function() {
|
|
|
|
that.$el.find('#settings__file-master-pass').focus();
|
|
|
|
}
|
2015-10-24 18:43:30 +02:00
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2015-10-18 16:02:00 +02:00
|
|
|
},
|
|
|
|
|
2015-12-08 22:00:31 +01:00
|
|
|
save: function(arg) {
|
|
|
|
var that = this;
|
|
|
|
if (!arg) {
|
|
|
|
arg = {};
|
|
|
|
}
|
|
|
|
arg.startedByUser = true;
|
|
|
|
if (!arg.skipValidation) {
|
|
|
|
var isValid = this.validatePassword(function() {
|
|
|
|
arg.skipValidation = true;
|
|
|
|
that.save(arg);
|
|
|
|
});
|
|
|
|
if (!isValid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-12-12 16:43:43 +01:00
|
|
|
this.appModel.syncFile(this.model, arg);
|
2015-12-08 22:00:31 +01:00
|
|
|
},
|
|
|
|
|
2015-12-08 20:18:35 +01:00
|
|
|
saveDefault: function() {
|
2015-12-08 22:00:31 +01:00
|
|
|
this.save();
|
2015-12-08 20:18:35 +01:00
|
|
|
},
|
|
|
|
|
2015-12-08 19:02:50 +01:00
|
|
|
saveToFile: function(skipValidation) {
|
2015-12-08 20:18:35 +01:00
|
|
|
if (skipValidation !== true && !this.validatePassword(this.saveToFile.bind(this, true))) {
|
2015-10-24 18:43:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-12-10 22:31:47 +01:00
|
|
|
var fileName = this.model.get('name') + '.kdbx';
|
2015-11-03 22:59:57 +01:00
|
|
|
var that = this;
|
2015-12-10 22:31:47 +01:00
|
|
|
if (Launcher && !this.model.get('storage')) {
|
|
|
|
Launcher.getSaveFileName(fileName, function (path) {
|
|
|
|
if (path) {
|
|
|
|
that.save({storage: 'file', path: path});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.model.getData(function (data) {
|
|
|
|
if (Launcher) {
|
|
|
|
Launcher.getSaveFileName(fileName, function (path) {
|
|
|
|
if (path) {
|
|
|
|
Storage.file.save(path, data, function (err) {
|
|
|
|
if (err) {
|
|
|
|
Alerts.error({
|
2015-12-17 19:25:25 +01:00
|
|
|
header: Locale.setFileSaveError,
|
|
|
|
body: Locale.setFileSaveErrorBody + ' ' + path + ': \n' + err
|
2015-12-10 22:31:47 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var blob = new Blob([data], {type: 'application/octet-stream'});
|
|
|
|
FileSaver.saveAs(blob, fileName);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-10-18 16:02:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
exportAsXml: function() {
|
2015-11-03 22:59:57 +01:00
|
|
|
this.model.getXml((function(xml) {
|
|
|
|
var blob = new Blob([xml], {type: 'text/xml'});
|
|
|
|
FileSaver.saveAs(blob, this.model.get('name') + '.xml');
|
|
|
|
}).bind(this));
|
2015-10-18 16:02:00 +02:00
|
|
|
},
|
|
|
|
|
2015-12-08 22:00:31 +01:00
|
|
|
saveToDropbox: function() {
|
2015-11-03 22:59:57 +01:00
|
|
|
var that = this;
|
2015-12-12 16:43:43 +01:00
|
|
|
this.model.set('syncing', true);
|
2015-12-08 22:00:31 +01:00
|
|
|
DropboxLink.authenticate(function(err) {
|
2015-12-12 16:43:43 +01:00
|
|
|
that.model.set('syncing', false);
|
2015-12-08 22:00:31 +01:00
|
|
|
if (err) {
|
|
|
|
return;
|
|
|
|
}
|
2015-12-12 16:43:43 +01:00
|
|
|
if (that.model.get('storage') === 'dropbox') {
|
|
|
|
that.save();
|
|
|
|
} else {
|
|
|
|
that.model.set('syncing', true);
|
2016-03-14 06:04:55 +01:00
|
|
|
DropboxLink.list('', function(err, files) { // TODO: replace with actual path
|
2015-12-12 16:43:43 +01:00
|
|
|
that.model.set('syncing', false);
|
|
|
|
if (!files) { return; }
|
|
|
|
var expName = that.model.get('name').toLowerCase();
|
|
|
|
var existingPath = files.filter(function(f) { return f.toLowerCase().replace('/', '') === expName; })[0];
|
|
|
|
if (existingPath) {
|
|
|
|
Alerts.yesno({
|
|
|
|
icon: 'dropbox',
|
2015-12-17 19:25:25 +01:00
|
|
|
header: Locale.setFileAlreadyExists,
|
|
|
|
body: Locale.setFileAlreadyExistsBody.replace('{}', that.model.escape('name')),
|
2015-12-12 16:43:43 +01:00
|
|
|
success: function() {
|
|
|
|
that.model.set('syncing', true);
|
|
|
|
DropboxLink.deleteFile(existingPath, function(err) {
|
|
|
|
that.model.set('syncing', false);
|
|
|
|
if (!err) {
|
|
|
|
that.save({storage: 'dropbox'});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
that.save({storage: 'dropbox'});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-11-03 22:59:57 +01:00
|
|
|
});
|
2015-10-22 20:03:44 +02:00
|
|
|
},
|
|
|
|
|
2015-11-07 21:37:54 +01:00
|
|
|
closeFile: function() {
|
|
|
|
if (this.model.get('modified')) {
|
|
|
|
var that = this;
|
|
|
|
Alerts.yesno({
|
2015-12-17 19:25:25 +01:00
|
|
|
header: Locale.setFileUnsaved,
|
|
|
|
body: Locale.setFileUnsavedBody,
|
2015-11-07 21:37:54 +01:00
|
|
|
buttons: [
|
2015-12-17 19:25:25 +01:00
|
|
|
{result: 'close', title: Locale.setFileCloseNoSave, error: true},
|
|
|
|
{result: '', title: Locale.setFileDontClose}
|
2015-11-07 21:37:54 +01:00
|
|
|
],
|
|
|
|
success: function(result) {
|
|
|
|
if (result === 'close') {
|
|
|
|
that.closeFileNoCheck();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.closeFileNoCheck();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
closeFileNoCheck: function() {
|
2015-12-08 20:18:35 +01:00
|
|
|
this.appModel.closeFile(this.model);
|
2015-11-07 21:37:54 +01:00
|
|
|
},
|
|
|
|
|
2015-10-24 18:43:30 +02:00
|
|
|
keyFileChange: function(e) {
|
2015-10-22 20:03:44 +02:00
|
|
|
switch (e.target.value) {
|
2015-10-24 18:43:30 +02:00
|
|
|
case 'old':
|
|
|
|
this.selectOldKeyFile();
|
2015-10-22 20:03:44 +02:00
|
|
|
break;
|
|
|
|
case 'gen':
|
|
|
|
this.generateKeyFile();
|
|
|
|
break;
|
2015-10-24 18:43:30 +02:00
|
|
|
case 'none':
|
2015-10-22 20:03:44 +02:00
|
|
|
this.clearKeyFile();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-10-24 18:43:30 +02:00
|
|
|
selectOldKeyFile: function() {
|
|
|
|
this.model.resetKeyFile();
|
|
|
|
this.renderKeyFileSelect();
|
2015-10-22 20:03:44 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
generateKeyFile: function() {
|
2015-10-24 18:43:30 +02:00
|
|
|
var keyFile = this.model.generateAndSetKeyFile();
|
|
|
|
var blob = new Blob([keyFile], {type: 'application/octet-stream'});
|
|
|
|
FileSaver.saveAs(blob, this.model.get('name') + '.key');
|
|
|
|
this.renderKeyFileSelect();
|
2015-10-22 20:03:44 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
clearKeyFile: function() {
|
2015-10-24 18:43:30 +02:00
|
|
|
this.model.removeKeyFile();
|
|
|
|
this.renderKeyFileSelect();
|
|
|
|
},
|
|
|
|
|
|
|
|
triggerSelectFile: function() {
|
|
|
|
this.$el.find('#settings__file-file-select').click();
|
|
|
|
},
|
|
|
|
|
|
|
|
fileSelected: function(e) {
|
|
|
|
var file = e.target.files[0];
|
|
|
|
var reader = new FileReader();
|
|
|
|
reader.onload = (function(e) {
|
|
|
|
var res = e.target.result;
|
|
|
|
this.model.setKeyFile(res, file.name);
|
|
|
|
this.renderKeyFileSelect();
|
|
|
|
}).bind(this);
|
|
|
|
reader.readAsArrayBuffer(file);
|
2015-10-22 22:39:30 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
focusMasterPass: function(e) {
|
2015-12-12 16:43:43 +01:00
|
|
|
e.target.value = '';
|
2015-10-23 22:12:12 +02:00
|
|
|
e.target.setAttribute('type', 'text');
|
2015-10-22 22:51:40 +02:00
|
|
|
},
|
|
|
|
|
2016-03-04 17:39:29 +01:00
|
|
|
changeMasterPass: function(e) {
|
2015-10-22 22:51:40 +02:00
|
|
|
if (!e.target.value) {
|
2015-10-24 18:43:30 +02:00
|
|
|
this.model.resetPassword();
|
|
|
|
this.$el.find('.settings__file-master-pass-warning').hide();
|
2015-10-23 22:12:12 +02:00
|
|
|
} else {
|
2015-10-24 18:43:30 +02:00
|
|
|
this.model.setPassword(kdbxweb.ProtectedValue.fromString(e.target.value));
|
|
|
|
if (!this.model.get('created')) {
|
|
|
|
this.$el.find('.settings__file-master-pass-warning').show();
|
|
|
|
}
|
2015-10-22 22:51:40 +02:00
|
|
|
}
|
2016-03-04 17:39:29 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
blurMasterPass: function(e) {
|
|
|
|
if (!e.target.value) {
|
|
|
|
this.model.resetPassword();
|
|
|
|
e.target.value = PasswordGenerator.present(this.model.get('passwordLength'));
|
|
|
|
this.$el.find('.settings__file-master-pass-warning').hide();
|
|
|
|
}
|
2015-10-23 22:12:12 +02:00
|
|
|
e.target.setAttribute('type', 'password');
|
2015-10-24 18:43:30 +02:00
|
|
|
},
|
|
|
|
|
2016-03-04 17:39:29 +01:00
|
|
|
changeName: function(e) {
|
2015-10-24 18:43:30 +02:00
|
|
|
var value = $.trim(e.target.value);
|
|
|
|
if (!value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.model.setName(value);
|
|
|
|
},
|
|
|
|
|
2016-03-04 17:39:29 +01:00
|
|
|
changeDefUser: function(e) {
|
2015-10-24 18:43:30 +02:00
|
|
|
var value = $.trim(e.target.value);
|
|
|
|
this.model.setDefaultUser(value);
|
|
|
|
},
|
|
|
|
|
|
|
|
changeTrash: function(e) {
|
|
|
|
this.model.setRecycleBinEnabled(e.target.checked);
|
|
|
|
},
|
|
|
|
|
2016-03-04 17:39:29 +01:00
|
|
|
changeHistoryLength: function(e) {
|
2015-10-24 18:43:30 +02:00
|
|
|
var value = +e.target.value;
|
|
|
|
if (isNaN(value)) {
|
|
|
|
e.target.value = this.model.get('historyMaxItems');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.model.setHistoryMaxItems(value);
|
|
|
|
},
|
|
|
|
|
2016-03-04 17:39:29 +01:00
|
|
|
changeHistorySize: function(e) {
|
2015-10-24 18:43:30 +02:00
|
|
|
var value = +e.target.value;
|
|
|
|
if (isNaN(value)) {
|
|
|
|
e.target.value = this.model.get('historyMaxSize') / 1024 / 1024;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.model.setHistoryMaxSize(value * 1024 * 1024);
|
|
|
|
},
|
|
|
|
|
2016-03-04 17:39:29 +01:00
|
|
|
changeKeyRounds: function(e) {
|
2015-10-24 18:43:30 +02:00
|
|
|
var value = +e.target.value;
|
|
|
|
if (isNaN(value)) {
|
|
|
|
e.target.value = this.model.get('keyEncryptionRounds');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.model.setKeyEncryptionRounds(value);
|
2015-10-17 23:49:24 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-03-14 06:04:55 +01:00
|
|
|
module.exports = SettingsFileView;
|