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

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;
2019-07-29 15:43:53 +02:00
};
2017-12-30 21:57:34 +02:00
shared.initState = function(comp, props) {
2019-07-29 15:43:53 +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);
2019-07-29 15:43:53 +02:00
};
2017-12-30 21:57:34 +02:00
shared.refreshStats = async function(comp) {
const stats = await BaseItem.encryptedItemsStats();
comp.setState({ stats: stats });
2019-07-29 15:43:53 +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 });
2019-07-29 15:43:53 +02:00
};
2017-12-30 21:57:34 +02:00
shared.decryptedStatText = function(comp) {
const stats = comp.state.stats;
2019-07-29 15:43:53 +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);
2019-07-29 15:43:53 +02:00
};
2017-12-30 21:57:34 +02:00
shared.onSavePasswordClick = function(comp, mk) {
const password = comp.state.passwords[mk.id];
if (!password) {
Setting.deleteObjectKey('encryption.passwordCache', mk.id);
2017-12-30 21:57:34 +02:00
} else {
Setting.setObjectKey('encryption.passwordCache', mk.id, password);
2017-12-30 21:57:34 +02:00
}
comp.checkPasswords();
2019-07-29 15:43:53 +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 });
2019-07-29 15:43:53 +02:00
};
2017-12-30 21:57:34 +02:00
2019-07-29 15:43:53 +02:00
module.exports = shared;