diff --git a/www/index.html b/www/index.html index 4472732..5b18c08 100644 --- a/www/index.html +++ b/www/index.html @@ -17,6 +17,7 @@ + diff --git a/www/js/accounts.js b/www/js/accounts.js new file mode 100644 index 0000000..c4c3831 --- /dev/null +++ b/www/js/accounts.js @@ -0,0 +1,131 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +userinfo = null; +theme = {"title": "Business", "color": "#ffffff", "bgcolor": ""}; +accountid = 0; + +/** + * Check if the accounts configuration seems valid. + * @returns {Boolean} true if valid, otherwise false + */ +function isconfigvalid() { + if (localStorage.getItem("accounts") !== null) { + if (JSON.parse(localStorage.getItem("accounts")).length > 0) { + return true; + } + } + return false; +} + +function getaccounts() { + if (isconfigvalid()) { + return JSON.parse(localStorage.getItem("accounts")); + } + return []; +} + +function saveaccounts(accounts) { + localStorage.setItem("accounts", JSON.stringify(accounts)); +} + +/** + * Switch to a different account. + * @param {int} account The selected account index from localStorage.getItem('accounts') + * @returns {undefined} + */ +function switchaccount(account) { + // If there isn't an accounts list yet, this shouldi take us back to the setup + if (!isconfigvalid()) { + restartApplication(); + } + var accounts = getaccounts(); + // Selected account doesn't exist, choose the first one instead + if (typeof accounts[account] === 'undefined') { + account = 0; + } + var accountinfo = accounts[account]; + localStorage.setItem("username", accountinfo['username']); + localStorage.setItem("password", accountinfo['password']); + localStorage.setItem("syncurl", accountinfo['syncurl']); + localStorage.setItem("key", accountinfo['key']); + accountid = account; +} + +/** + * Add a Business Apps account. + * @param {String} username + * @param {String} password + * @param {String} url + * @param {String} key + * @returns {Number} The index of the new account + */ +function addaccount(username, password, url, key) { + var accounts = getaccounts(); + accounts.push({"username": username, "password": password, "syncurl": url, "key": key}); + saveaccounts(accounts); + return accounts.length - 1; +} + +/** + * Remove a Business Apps account. + * @param {Number} Account index + */ +function rmaccount(id) { + var accounts = getaccounts(); + accounts.splice(id, 1); + saveaccounts(accounts); + if (id == accountid) { + switchaccount(0); + } +} + +/** + * Fetch user info (name, email, etc) from the server and save to the global + * variable `userinfo`. + * @param function callback An optional function to run if/when the request + * succeeds. + */ +function getuserinfo(callback) { + $(".loading-text").text("Loading account..."); + if (localStorage.getItem("username") === null + || localStorage.getItem("password") === null + || localStorage.getItem("syncurl") === null + || localStorage.getItem("key") === null) { + switchaccount(0); + } + $.post(localStorage.getItem("syncurl"), { + username: localStorage.getItem("username"), + key: localStorage.getItem("key"), + password: localStorage.getItem("password"), + action: "user_info" + }, function (data) { + if (data.status === 'OK') { + $(".loading-text").text("Loading..."); + userinfo = data.info; + if (typeof callback == 'function') { + callback(); + } + } else { + navigator.notification.alert(data.msg, null, "Error", 'Dismiss'); + openscreen("homeloaderror"); + } + }, "json").fail(function () { + navigator.notification.alert("Could not connect to the server. Try again later.", null, "Error", 'Dismiss'); + openscreen("homeloaderror"); + }); +} + +/** + * Set a new password for the current account + * @param {String} newpass + * @returns {undefined} + */ +function passwd(newpass) { + var accounts = getaccounts(); + accounts[accountid]["password"] = newpass; + saveaccounts(accounts); +} \ No newline at end of file diff --git a/www/js/app.js b/www/js/app.js index 59e700c..da47127 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -2,8 +2,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -userinfo = null; - /** * Switches the app to the given screen. * @param {String} screenname The name of the screen to show. @@ -29,35 +27,6 @@ function openscreen(screenname, effect) { currentscreen = screenname; } -/** - * Fetch user info (name, email, etc) from the server and save to the global - * variable `userinfo`. - * @param function callback An optional function to run if/when the request - * succeeds. - */ -function getuserinfo(callback) { - $(".loading-text").text("Loading account..."); - $.post(localStorage.getItem("syncurl"), { - username: localStorage.getItem("username"), - key: localStorage.getItem("key"), - password: localStorage.getItem("password"), - action: "user_info" - }, function (data) { - if (data.status === 'OK') { - $(".loading-text").text("Loading..."); - userinfo = data.info; - if (typeof callback == 'function') { - callback(); - } - } else { - navigator.notification.alert(data.msg, null, "Error", 'Dismiss'); - openscreen("homeloaderror"); - } - }, "json").fail(function () { - navigator.notification.alert("Could not connect to the server. Try again later.", null, "Error", 'Dismiss'); - openscreen("homeloaderror"); - }); -} function openfragment(fragment, target, effect) { if (effect === 'FADE') { @@ -110,11 +79,11 @@ function setnavbartitle(title, showarrow, backscreen) { /** * Set the navbar. - * @param String,boolean type false if no navbar, "home" for the home screen, + * @param {String,boolean} type false if no navbar, "home" for the home screen, * "settings" for settings, "app" for a custom title, or anything else for * a simple one. - * @param String screentitle The title to show if type == "app" - * @param String returnscreen Where to go back to. Defaults to "home". + * @param {String} screentitle The title to show if type == "app" + * @param {String} returnscreen Where to go back to. Defaults to "home". * @returns {undefined} */ function setnavbar(type, screentitle, returnscreen) { @@ -231,7 +200,7 @@ function restartApplication() { // Handle back button to close things document.addEventListener("backbutton", function (event) { - if (localStorage.getItem("setupcomplete")) { + if (isconfigvalid()) { if ($("#appframe").length && historyctr > 0) { console.log("going back"); var iframe = document.getElementById("appframe"); @@ -264,7 +233,7 @@ document.addEventListener("deviceready", function () { $(this).parent().fadeOut("slow"); }); - if (localStorage.getItem("setupcomplete")) { + if (isconfigvalid()) { getuserinfo(function () { openscreen("home"); }); diff --git a/www/views/accounts.html b/www/views/accounts.html new file mode 100644 index 0000000..0ddcfd9 --- /dev/null +++ b/www/views/accounts.html @@ -0,0 +1,61 @@ + +
+ +
+
+
+
+
+ +

+

You haven't added any Business Apps accounts yet. Press to add one.

+
+
+
+
+
+
+ diff --git a/www/views/home.html b/www/views/home.html index c26e37d..f2328b6 100644 --- a/www/views/home.html +++ b/www/views/home.html @@ -40,7 +40,7 @@ }); } - if (localStorage.getItem('setupcomplete')) { + if (isconfigvalid()) { setnavbar("home"); loadapps(); } else { diff --git a/www/views/settings.html b/www/views/settings.html index 74e0884..662eb09 100644 --- a/www/views/settings.html +++ b/www/views/settings.html @@ -7,6 +7,9 @@ Pairing Code: None
Server: None +
+ Manage and switch accounts +
@@ -15,8 +18,8 @@

Enter your new password if you changed it from AccountHub.

- Log out -

Forget all cached account data (including sync key) and open the setup tool.

+ Disconnect All Accounts +

Forget all account data for all connected accounts and open the setup tool.

@@ -63,13 +66,13 @@ return; } // Wipe localStorage - localStorage.removeItem("setupcomplete"); localStorage.removeItem("username"); localStorage.removeItem("password"); localStorage.removeItem("syncurl"); localStorage.removeItem("key"); + localStorage.removeItem("accounts"); // force-reload app - navigator.notification.alert("Connection data and credentials erased.", function () { + navigator.notification.alert("All connection data and credentials erased.", function () { restartApplication(); }, "App Reset", 'Continue'); }, "Are you sure?"); @@ -90,7 +93,7 @@ } /** - * Prompts the user to enter their password, then checks the password and + * Prompts the user to enter their password, then checks the password and * saves or displays an error. * @returns {undefined} */ @@ -105,7 +108,7 @@ }, function (data) { if (data.status === 'OK') { localStorage.setItem("password", results.input1); - localStorage.setItem("setupcomplete", true); + passwd(results.input1); navigator.notification.alert("Saved password updated.", function () { // Reload app just to be safe restartApplication(); @@ -133,7 +136,7 @@ cordova.getAppVersion.getVersionCode(function (version) { $('#app_version_code').text(version); }); - + if (localStorage.getItem("username")) { $("#username").text(localStorage.getItem("username")); } diff --git a/www/views/setup1.html b/www/views/setup1.html index 4328472..d7a2b62 100644 --- a/www/views/setup1.html +++ b/www/views/setup1.html @@ -4,12 +4,15 @@
-

Setup

+

Add Account

-

Welcome! There's a few things we need to do to get everything ready. -

- Open AccountHub on another device and go to your account settings. Generate a mobile sync code, then press the button below to scan it. +

+ + Open AccountHub on another device and go to Sync settings. Generate a mobile sync code, then press the button below to scan it.

Scan Code @@ -38,6 +41,16 @@