Changes on promises

keepass importer ready and using promises
This commit is contained in:
Marcos Zuriaga 2016-09-26 17:05:47 +02:00
parent 349043c2c3
commit 8db3f31367
4 changed files with 53 additions and 44 deletions

View file

@ -82,7 +82,10 @@ angular.module('passmanApp')
$scope.startImport = function(){
if(file_data){
$window.PassmanImporter[$scope.selectedImporter.id].readFile(file_data, function(parseddata){
$window.PassmanImporter[$scope.selectedImporter.id]
.readFile(file_data)
.then(function(parseddata){
console.log("woof!");
parsed_data = parseddata;
_log('Parsed '+ parsed_data.length + ' credentials, starting to import');
$scope.current_import_length = parsed_data.length;
@ -91,9 +94,9 @@ angular.module('passmanApp')
} else {
// @TODO Show message no data found
}
}).progress(function(percentage){
console.log(percentage + '%');
});
}
}

View file

@ -26,7 +26,7 @@ angular.module('passmanApp')
});
},
generateRSAKeys: function(key_length, progress, callback){
var p = new C_Promise(function(promise){
var p = new C_Promise(function(){
var state = forge.pki.rsa.createKeyPairGenerationState(key_length, 0x10001);
var step = function() {
// run for 100 ms
@ -34,32 +34,32 @@ angular.module('passmanApp')
// console.log(state);
if (state.p !== null) {
// progress(50);
promise.call_progress(50);
this.call_progress(50);
}
else {
// progress(0);
promise.call_progress(50);
this.call_progress(0);
}
setTimeout(step, 1);
setTimeout(step.bind(this), 1);
}
else {
// callback(state.keys);
promise.call_then(state.keys);
this.call_then(state.keys);
}
};
setTimeout(step, 100);
setTimeout(step.bind(this), 100);
});
return p;
},
generateSharedKey: function(size){
size = size || 20;
return new C_Promise(function(promise){ /** prmise C_Promise **/
return new C_Promise(function(){
CRYPTO.PASSWORD.generate(size,
function(pass) {
promise.call_then(pass);
this.call_then(pass);
},
function(progress) {
promise.call_progress(progress);
this.call_progress(progress);
}
);
})

View file

@ -12,28 +12,32 @@ PassmanImporter.keepassCsv = {
};
PassmanImporter.keepassCsv.readFile = function (file_data, callback) {
var parsed_csv = PassmanImporter.readCsv(file_data);
var credential_list = [];
for (var i = 0; i < parsed_csv.length; i++) {
var row = parsed_csv[i];
var _credential = PassmanImporter.newCredential();
_credential.label = row.account;
_credential.username = row.login_name;
_credential.password = row.password;
_credential.url = row.web_site;
if (row.hasOwnProperty('expires')) {
row.expires = row.expires.replace('"','');
_credential.expire_time = new Date(row.expires).getTime() / 1000;
}
var tags = [{text: row.group}];
if (row.hasOwnProperty('group_tree')) {
var exploded_tree = row.group_tree.split('\\\\');
for (var t = 0; t < exploded_tree.length; t++) {
tags.push({text: exploded_tree[t]});
var p = new C_Promise(function(){
var parsed_csv = PassmanImporter.readCsv(file_data);
var credential_list = [];
for (var i = 0; i < parsed_csv.length; i++) {
var row = parsed_csv[i];
var _credential = PassmanImporter.newCredential();
_credential.label = row.account;
_credential.username = row.login_name;
_credential.password = row.password;
_credential.url = row.web_site;
if (row.hasOwnProperty('expires')) {
row.expires = row.expires.replace('"','');
_credential.expire_time = new Date(row.expires).getTime() / 1000;
}
var tags = [{text: row.group}];
if (row.hasOwnProperty('group_tree')) {
var exploded_tree = row.group_tree.split('\\\\');
for (var t = 0; t < exploded_tree.length; t++) {
tags.push({text: exploded_tree[t]});
}
}
_credential.tags = tags;
credential_list.push(_credential);
this.call_progress(i/parsed_csv.length*100);
}
_credential.tags = tags;
credential_list.push(_credential);
}
callback(credential_list);
this.call_then(credential_list);
});
return p;
};

View file

@ -9,26 +9,28 @@
*/
function C_Promise(workload){
this.update = null; this.finally = null; this.error_function = null;
this.then = function(callback){
this.finally = callback;
return this;
}
};
this.progress = function(callback){
this.update = callback;
return this;
}
};
this.error = function (callback){
this.error_function = callback;
return this;
}
};
this.call_then = function(data){
if (this.finally !== undefined) this.finally(data);
}
if (this.finally !== null) this.finally(data);
};
this.call_progress = function(data){
if (this.progress !== undefined) this.progress(data);
}
if (this.update !== null) this.update(data);
};
this.call_error = function(data){
if(this.error_function !== undefined) this.error_function(data);
}
setTimeout(workload(this), 100);
if(this.error_function !== null) this.error_function(data);
};
setTimeout(workload.bind(this), 100);
}