Wallet/js/controllers/backup.js

77 lines
2.3 KiB
JavaScript
Raw Normal View History

2014-03-26 09:18:42 -03:00
'use strict';
2014-06-03 17:42:36 -03:00
angular.module('copayApp.controllers').controller('BackupController',
2014-06-16 15:51:19 -03:00
function($scope, $rootScope, $location, $window, $timeout, $modal, controllerUtils, walletFactory) {
2014-03-26 09:18:42 -03:00
$scope.title = 'Backup';
2014-04-17 18:02:20 -03:00
var _getEncryptedWallet = function() {
2014-05-01 18:33:36 -03:00
var wallet = $rootScope.wallet.toEncryptedObj();
2014-04-17 18:02:20 -03:00
return wallet;
};
2014-05-09 09:52:02 -03:00
$scope.download = function() {
var timestamp = +(new Date);
2014-06-16 15:51:19 -03:00
var walletName = ($rootScope.wallet.name ? $rootScope.wallet.name : '') + '-' + $rootScope.wallet.id;
2014-05-09 09:52:02 -03:00
var filename = walletName + '-' + timestamp + '.json.aes';
2014-04-17 18:02:20 -03:00
var wallet = _getEncryptedWallet();
2014-06-16 15:51:19 -03:00
var blob = new Blob([wallet], {
type: 'text/plain;charset=utf-8'
});
// show a native save dialog if we are in the shell
// and pass the wallet to the shell to convert to node Buffer
if (window.cshell) {
return window.cshell.send('backup:download', {
name: walletName,
wallet: wallet
});
}
// otherwise lean on the browser implementation
2014-04-17 18:02:20 -03:00
saveAs(blob, filename);
};
2014-06-16 15:51:19 -03:00
$scope.openModal = function() {
var modalInstance = $modal.open({
templateUrl: 'backupModal.html',
controller: ModalInstanceCtrl,
});
2014-06-03 16:13:56 -03:00
2014-06-16 15:51:19 -03:00
modalInstance.result.then(sendEmail);
};
2014-06-03 16:13:56 -03:00
2014-06-16 15:51:19 -03:00
$scope.deleteWallet = function() {
var w=$rootScope.wallet;
w.disconnect();
walletFactory.remove(w.id, function() {
controllerUtils.logout();
});
};
2014-06-03 16:13:56 -03:00
2014-06-16 15:51:19 -03:00
var sendEmail = function(email) {
var body = _getEncryptedWallet();
var subject = ($rootScope.wallet.name ? $rootScope.wallet.name + ' - ' : '') + $rootScope.wallet.id;
var href = 'mailto:' + email + '?' + 'subject=[Copay Backup] ' + subject + '&' + 'body=' + body;
2014-06-16 15:51:19 -03:00
if (window.cshell) {
return window.cshell.send('backup:email', href);
}
2014-06-03 16:13:56 -03:00
2014-06-16 15:51:19 -03:00
var newWin = $window.open(href, '_blank', 'scrollbars=yes,resizable=yes,width=10,height=10');
if (newWin) {
$timeout(function() {
newWin.close();
}, 1000);
}
};
});
2014-06-03 16:13:56 -03:00
2014-06-16 15:51:19 -03:00
var ModalInstanceCtrl = function($scope, $modalInstance) {
2014-06-03 16:13:56 -03:00
2014-06-16 15:51:19 -03:00
$scope.submit = function(form) {
2014-06-03 16:13:56 -03:00
$modalInstance.close($scope.email);
};
2014-06-16 15:51:19 -03:00
$scope.cancel = function() {
2014-06-03 16:13:56 -03:00
$modalInstance.dismiss('cancel');
};
};