Wallet/src/js/controllers/preferences.js

148 lines
4.5 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('preferencesController',
2015-08-27 12:07:13 -03:00
function($scope, $rootScope, $filter, $timeout, $modal, $log, lodash, configService, profileService, uxLanguage) {
2015-10-07 16:17:19 -03:00
this.init = function() {
var config = configService.getSync();
this.unitName = config.wallet.settings.unitName;
this.bwsurl = config.bws.url;
this.currentLanguageName = uxLanguage.getCurrentLanguageName();
this.selectedAlternative = {
name: config.wallet.settings.alternativeName,
isoCode: config.wallet.settings.alternativeIsoCode
};
$scope.spendUnconfirmed = config.wallet.spendUnconfirmed;
$scope.glideraEnabled = config.glidera.enabled;
$scope.glideraTestnet = config.glidera.testnet;
var fc = profileService.focusedClient;
if (fc) {
$scope.encrypt = fc.hasPrivKeyEncrypted();
this.externalSource = fc.getPrivKeyExternalSourceName() == 'ledger' ? "Ledger" : null;
// TODO externalAccount
//this.externalIndex = fc.getExternalIndex();
}
if (window.touchidAvailable) {
var walletId = fc.credentials.walletId;
this.touchidAvailable = true;
config.touchIdFor = config.touchIdFor || {};
$scope.touchid = config.touchIdFor[walletId];
}
};
2015-03-06 12:00:10 -03:00
2015-08-03 20:39:09 -03:00
var unwatchSpendUnconfirmed = $scope.$watch('spendUnconfirmed', function(newVal, oldVal) {
if (newVal == oldVal) return;
var opts = {
wallet: {
spendUnconfirmed: newVal
}
};
configService.set(opts, function(err) {
2015-08-11 17:45:57 -03:00
$rootScope.$emit('Local/SpendUnconfirmedUpdated');
2015-08-03 20:39:09 -03:00
if (err) $log.debug(err);
});
});
2015-03-06 12:00:10 -03:00
var unwatch = $scope.$watch('encrypt', function(val) {
var fc = profileService.focusedClient;
2015-05-13 12:41:05 -03:00
if (!fc) return;
2015-03-06 12:00:10 -03:00
if (val && !fc.hasPrivKeyEncrypted()) {
$rootScope.$emit('Local/NeedsPassword', true, function(err, password) {
if (err || !password) {
$scope.encrypt = false;
return;
}
profileService.setPrivateKeyEncryptionFC(password, function() {
2015-11-10 20:05:05 -03:00
$rootScope.$emit('Local/NewEncryptionSetting');
2015-03-06 12:00:10 -03:00
$scope.encrypt = true;
});
});
} else {
if (!val && fc.hasPrivKeyEncrypted()) {
profileService.unlockFC(function(err){
if (err) {
$scope.encrypt = true;
return;
}
profileService.disablePrivateKeyEncryptionFC(function(err) {
2015-11-10 20:05:05 -03:00
$rootScope.$emit('Local/NewEncryptionSetting');
2015-03-06 12:00:10 -03:00
if (err) {
$scope.encrypt = true;
$log.error(err);
return;
}
$scope.encrypt = false;
});
});
}
}
});
2015-09-11 13:11:41 -03:00
var unwatchGlideraEnabled = $scope.$watch('glideraEnabled', function(newVal, oldVal) {
if (newVal == oldVal) return;
var opts = {
glidera: {
enabled: newVal
}
};
configService.set(opts, function(err) {
$rootScope.$emit('Local/GlideraUpdated');
if (err) $log.debug(err);
});
});
var unwatchGlideraTestnet = $scope.$watch('glideraTestnet', function(newVal, oldVal) {
if (newVal == oldVal) return;
var opts = {
glidera: {
testnet: newVal
}
};
configService.set(opts, function(err) {
$rootScope.$emit('Local/GlideraUpdated');
if (err) $log.debug(err);
});
});
2015-10-07 16:17:19 -03:00
var unwatchRequestTouchid = $scope.$watch('touchid', function(newVal, oldVal) {
if (newVal == oldVal || $scope.touchidError) {
$scope.touchidError = false;
return;
}
var walletId = profileService.focusedClient.credentials.walletId;
var opts = {
touchIdFor: {}
};
opts.touchIdFor[walletId] = newVal;
$rootScope.$emit('Local/RequestTouchid', function(err) {
if (err) {
$log.debug(err);
$timeout(function() {
$scope.touchidError = true;
$scope.touchid = oldVal;
}, 100);
}
else {
configService.set(opts, function(err) {
if (err) {
$log.debug(err);
$scope.touchidError = true;
$scope.touchid = oldVal;
}
});
}
});
});
2015-03-06 12:00:10 -03:00
$scope.$on('$destroy', function() {
unwatch();
2015-08-03 20:39:09 -03:00
unwatchSpendUnconfirmed();
2015-09-11 13:11:41 -03:00
unwatchGlideraEnabled();
unwatchGlideraTestnet();
2015-10-07 16:17:19 -03:00
unwatchRequestTouchid();
2015-03-06 12:00:10 -03:00
});
});