forked from Netsyms/PackageHelper
Add settings sync/backup feature
parent
424c6b3987
commit
68117dda32
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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/.
|
||||
*/
|
||||
|
||||
|
||||
$(window).on("message", function (e) {
|
||||
var data = e.originalEvent.data;
|
||||
var parts = data.split("&");
|
||||
var u = "";
|
||||
var p = "";
|
||||
|
||||
for (var i = 0; i < parts.length; i++) {
|
||||
if (parts[i].startsWith("user:")) {
|
||||
u = parts[i].replace("user:", "");
|
||||
} else if (parts[i].startsWith("password:")) {
|
||||
p = parts[i].replace("password:", "");
|
||||
}
|
||||
}
|
||||
|
||||
if (u != "" && p != "") {
|
||||
setStorage("username", u);
|
||||
setStorage("password", p);
|
||||
app.toast.show({
|
||||
text: "You are now logged in!",
|
||||
position: "bottom",
|
||||
destroyOnClose: true,
|
||||
closeTimeout: 1000 * 3
|
||||
});
|
||||
restartApplication();
|
||||
} else {
|
||||
app.dialog.alert("There was a problem. Try again later.", "Error");
|
||||
}
|
||||
});
|
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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/.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Save something to persistent storage.
|
||||
* @param {string} key
|
||||
* @param {string} value non-string values are converted to strings.
|
||||
* @param {bool} nochangeupdate If true, the lastchange setting won't be updated.
|
||||
* @returns {undefined}
|
||||
*/
|
||||
function setStorage(key, value, nochangeupdate) {
|
||||
if (typeof nochangeupdate == 'undefined') {
|
||||
nochangeupdate = false;
|
||||
}
|
||||
localStorage.setItem(key, value);
|
||||
if (!nochangeupdate && !SETTINGS.synckeyblacklist.includes(key)) {
|
||||
localStorage.setItem("lastchange", Date.now() / 1000);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an item from persistent storage.
|
||||
* @param {type} key
|
||||
* @returns {DOMString}
|
||||
*/
|
||||
function getStorage(key) {
|
||||
return localStorage.getItem(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an item is in the persistent storage.
|
||||
* @param {string} key
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
function inStorage(key) {
|
||||
return localStorage.getItem(key) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all item from persistent storage.
|
||||
* @returns {Array} [{key: "", value: ""},...]
|
||||
*/
|
||||
function getAllStorage() {
|
||||
var all = [];
|
||||
for (var key in localStorage) {
|
||||
if (localStorage.hasOwnProperty(key)) {
|
||||
all.push({
|
||||
key: key,
|
||||
value: getStorage(key)
|
||||
});
|
||||
}
|
||||
}
|
||||
return all;
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* 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/.
|
||||
*/
|
||||
|
||||
|
||||
function gatherSyncData() {
|
||||
var data = {
|
||||
localStorage: {},
|
||||
changed: getStorage("lastchange") == null ? 0 : getStorage("lastchange")
|
||||
};
|
||||
var allitems = getAllStorage();
|
||||
for (var i = 0; i < allitems.length; i++) {
|
||||
var key = allitems[i].key;
|
||||
var value = allitems[i].value;
|
||||
if (SETTINGS.synckeyblacklist.includes(key)) {
|
||||
continue;
|
||||
}
|
||||
data.localStorage[key] = value;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
function syncDataToLocalStorage(data) {
|
||||
for (var key in data.localStorage) {
|
||||
if (data.localStorage.hasOwnProperty(key)) {
|
||||
setStorage(key, data.localStorage[key], true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function resolveSync(remotedata) {
|
||||
var localchangetime = getStorage("lastchange");
|
||||
if (remotedata.changed == null) {
|
||||
// The server has nothing, this is the first sync
|
||||
return true;
|
||||
}
|
||||
if (localchangetime == null) {
|
||||
// No local setting changes but since we've gotten this far,
|
||||
// the server has stuff for us
|
||||
syncDataToLocalStorage(remotedata);
|
||||
return true;
|
||||
}
|
||||
if (localchangetime < remotedata.changed) {
|
||||
// The server has newer stuff for us
|
||||
syncDataToLocalStorage(remotedata);
|
||||
return true;
|
||||
}
|
||||
if (localchangetime >= remotedata.changed) {
|
||||
// Our local data is newer or the same as the server copy
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function syncNow(callback) {
|
||||
var username = getStorage("username");
|
||||
var password = getStorage("password");
|
||||
if (username == null || password == null) {
|
||||
return false;
|
||||
}
|
||||
var data = gatherSyncData();
|
||||
|
||||
$.post(SETTINGS.syncapi, {
|
||||
username: username,
|
||||
password: password,
|
||||
data: JSON.stringify(data)
|
||||
}, function (resp) {
|
||||
if (resp.status == "OK") {
|
||||
resolveSync(resp.data);
|
||||
setStorage("lastsync", Date.now() / 1000);
|
||||
if (typeof callback == "function") {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
}, "json");
|
||||
return true;
|
||||
}
|
||||
|
||||
function loadSettings() {
|
||||
if (getStorage("darktheme") == "true") {
|
||||
$("#app").addClass("theme-dark");
|
||||
} else {
|
||||
$("#app").removeClass("theme-dark");
|
||||
}
|
||||
|
||||
if (platform_type == "cordova") {
|
||||
if (getStorage("wakelock") == "true") {
|
||||
window.powerManagement.acquire(function () {
|
||||
console.log("Info", 'Wakelock acquired');
|
||||
}, function () {
|
||||
console.log("Warn", 'Failed to acquire wakelock');
|
||||
});
|
||||
} else {
|
||||
window.powerManagement.release(function () {
|
||||
console.log("Info", 'Wakelock released');
|
||||
}, function () {
|
||||
console.log("Warn", 'Failed to release wakelock');
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
syncNow(loadSettings);
|
||||
|
||||
// Sync every two minutes
|
||||
setInterval(function () {
|
||||
syncNow(loadSettings);
|
||||
}, 1000 * 60 * 2);
|
@ -0,0 +1,24 @@
|
||||
<!-- 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/. -->
|
||||
|
||||
<div class="page" data-name="login">
|
||||
|
||||
<div class="navbar">
|
||||
<div class="navbar-bg"></div>
|
||||
<div class="navbar-inner">
|
||||
<div class="left">
|
||||
<a href="#" class="link icon-only back">
|
||||
<i class="icon icon-back"></i>
|
||||
</a>
|
||||
</div>
|
||||
<div class="title">Log In</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="page-content">
|
||||
<iframe src="{{loginurl}}" id="loginframe" style="border: 0; width: 100%; height: calc(100vh - var(--f7-navbar-height));"></iframe>
|
||||
</div>
|
||||
|
||||
<script src="assets/js/login.js"></script>
|
||||
</div>
|
Loading…
Reference in New Issue