1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/ReactNativeClient/lib/components/shared/encryption-config-shared.js

92 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-03-09 19:49:35 +02:00
const EncryptionService = require("lib/services/EncryptionService");
const { _ } = require("lib/locale.js");
const BaseItem = require("lib/models/BaseItem.js");
const Setting = require("lib/models/Setting.js");
2017-12-30 21:57:34 +02:00
const shared = {};
shared.constructor = function(comp) {
comp.state = {
masterKeys: [],
passwords: {},
passwordChecks: {},
stats: {
encrypted: null,
total: null,
},
};
comp.isMounted_ = false;
comp.refreshStatsIID_ = null;
2018-03-09 19:49:35 +02:00
};
2017-12-30 21:57:34 +02:00
shared.initState = function(comp, props) {
2018-03-09 19:49:35 +02:00
comp.setState(
{
masterKeys: props.masterKeys,
passwords: props.passwords ? props.passwords : {},
},
() => {
comp.checkPasswords();
}
);
2017-12-30 21:57:34 +02:00
comp.refreshStats();
if (comp.refreshStatsIID_) {
clearInterval(comp.refreshStatsIID_);
comp.refreshStatsIID_ = null;
}
comp.refreshStatsIID_ = setInterval(() => {
if (!comp.isMounted_) {
clearInterval(comp.refreshStatsIID_);
comp.refreshStatsIID_ = null;
return;
}
comp.refreshStats();
}, 3000);
2018-03-09 19:49:35 +02:00
};
2017-12-30 21:57:34 +02:00
shared.refreshStats = async function(comp) {
const stats = await BaseItem.encryptedItemsStats();
comp.setState({ stats: stats });
2018-03-09 19:49:35 +02:00
};
2017-12-30 21:57:34 +02:00
shared.checkPasswords = async function(comp) {
const passwordChecks = Object.assign({}, comp.state.passwordChecks);
for (let i = 0; i < comp.state.masterKeys.length; i++) {
const mk = comp.state.masterKeys[i];
const password = comp.state.passwords[mk.id];
const ok = password ? await EncryptionService.instance().checkMasterKeyPassword(mk, password) : false;
passwordChecks[mk.id] = ok;
}
comp.setState({ passwordChecks: passwordChecks });
2018-03-09 19:49:35 +02:00
};
2017-12-30 21:57:34 +02:00
shared.decryptedStatText = function(comp) {
const stats = comp.state.stats;
2018-03-09 19:49:35 +02:00
const doneCount = stats.encrypted !== null ? stats.total - stats.encrypted : "-";
const totalCount = stats.total !== null ? stats.total : "-";
return _("Decrypted items: %s / %s", doneCount, totalCount);
};
2017-12-30 21:57:34 +02:00
shared.onSavePasswordClick = function(comp, mk) {
const password = comp.state.passwords[mk.id];
if (!password) {
2018-03-09 19:49:35 +02:00
Setting.deleteObjectKey("encryption.passwordCache", mk.id);
2017-12-30 21:57:34 +02:00
} else {
2018-03-09 19:49:35 +02:00
Setting.setObjectKey("encryption.passwordCache", mk.id, password);
2017-12-30 21:57:34 +02:00
}
comp.checkPasswords();
2018-03-09 19:49:35 +02:00
};
2017-12-30 21:57:34 +02:00
shared.onPasswordChange = function(comp, mk, password) {
const passwords = comp.state.passwords;
passwords[mk.id] = password;
comp.setState({ passwords: passwords });
2018-03-09 19:49:35 +02:00
};
2017-12-30 21:57:34 +02:00
2018-03-09 19:49:35 +02:00
module.exports = shared;