Cable-Desktop/js/options.js
lilia 9b12d8a978 Reconnect provisioning socket
If the provisioning socket closes, reconnect and generate a new qr code.
Fixes #371

// FREEBIE
2015-10-23 18:33:07 -07:00

100 lines
3.6 KiB
JavaScript

/*
* vim: ts=4:sw=4:expandtab
*/
;(function() {
'use strict';
extension.windows.getBackground(function(bg) {
$('.notifications .on button').click(function() {
bg.Whisper.Notifications.disable();
initOptions();
});
$('.notifications .off button').click(function() {
bg.Whisper.Notifications.enable(initOptions);
initOptions();
});
function initOptions() {
if (bg.Whisper.Notifications.isEnabled()) {
$('.notifications .on').show();
$('.notifications .off').hide();
} else {
$('.notifications .on').hide();
$('.notifications .off').show();
}
}
function setProvisioningUrl(url) {
$('#status').text('');
new QRCode($('#qr')[0]).makeCode(url);
}
function confirmNumber(number) {
return new Promise(function(resolve, reject) {
$('#qr').hide();
$('.confirmation-dialog .number').text(number);
$('.confirmation-dialog .cancel').click(function(e) {
reject();
});
$('.confirmation-dialog .ok').click(function(e) {
e.stopPropagation();
var name = $('#device-name').val();
if (name.trim().length === 0) {
return;
}
$('.confirmation-dialog').hide();
$('.progress-dialog').show();
$('.progress-dialog .status').text('Generating Keys');
resolve(name);
});
$('.modal-container').show();
});
}
var counter = 0;
function incrementCounter() {
$('.progress-dialog .bar').css('width', (++counter * 100 / 100) + '%');
}
$('.modal-container .cancel').click(function() {
$('.modal-container').hide();
});
$(function() {
var accountManager = new bg.getAccountManager();
var init = function() {
$('#init-setup').show().addClass('in');
$('#qr').html('');
$('#status').text("Connecting...");
accountManager.registerSecondDevice(setProvisioningUrl, confirmNumber, incrementCounter).then(function() {
var launch = function() {
bg.openInbox();
bg.removeEventListener('textsecure:contactsync', launch);
clearTimeout(timeout);
window.close();
};
var timeout = setTimeout(launch, 60000);
bg.addEventListener('textsecure:contactsync', launch);
$('.progress-dialog .status').text('Syncing groups and contacts');
$('.progress-dialog .bar').addClass('progress-bar-striped active');
}).catch(function(e) {
if (e.message === 'websocket closed') {
init();
} else if (e.name === 'HTTPError' && e.code == 411) {
$('.progress-dialog').hide();
$('.error-dialog').show();
$('.error-dialog .ok').click(function(e) {
chrome.runtime.reload();
});
}
else {
throw e;
}
});
};
init();
});
});
})();