jam-cloud/app/assets/javascripts/clientUpdate.js

134 lines
5.7 KiB
JavaScript

(function(context,$) {
"use strict";
context.JK = context.JK || {};
context.JK.ClientUpdate = function() {
var self = this;
var logger = context.JK.logger;
// updated once a download is started
var updateSize = 0;
/***************************************/
/******** CALLBACKS FROM BACKEND *******/
/***************************************/
function clientUpdateDownloadProgress(bytesReceived, bytesTotal, downloadSpeedMegSec, timeRemaining) {
//logger.debug("bytesReceived: " + bytesReceived, ", bytesTotal: " + bytesTotal, ", downloadSpeed: " + downloadSpeedMegSec, ", timeRemaining: " + timeRemaining );
// bytesTotal from Qt is not trust worthy; trust server's answer instead
$('#downloadprogressbar div').width( ((bytesReceived/updateSize) * 100).toString() + "%" )
$("#progres sbar_detail").text(parseInt(bytesReceived) + "/" + parseInt(updateSize))
}
function clientUpdateDownloadSuccess(updateLocation) {
logger.debug("client update downloaded successfully to: " + updateLocation);
startUpdate(updateLocation);
}
function clientUpdateDownloadFailure(errorMsg) {
logger.error("client update download error: " + errorMsg)
alert("Unable to download client update due to reason:" + errorMsg);
}
function clientUpdateLaunchSuccess(updateLocation) {
logger.debug("client update launched successfully to: " + updateLocation);
alert("Client updating momentarily...");
}
function clientUpdateLaunchFailure(errorMsg) {
logger.error("client update launch error: " + errorMsg)
alert("Unable to launch client updater due to reason: " + errorMsg)
}
/********************************************/
/******** END: CALLBACKS FROM BACKEND *******/
/********************************************/
function shouldUpdate(currentVersion, version) {
return true;
}
// check if updated is needed
function check() {
var product = "JamClient"
var os = context.jamClient.GetOSAsString()
var currentVersion = context.jamClient.ClientUpdateVersion();
if(currentVersion == null || currentVersion.indexOf("Compiled") > -1) {
// this is a developer build; it doesn't make much sense to do an packaged update, so skip
logger.debug("skipping client update check because this is a development build")
return;
}
$.ajax({
type: "GET",
url: "/api/versioncheck?product=" + product + "&os=" + os,
success: function(response) {
var version = response.version;
logger.debug("our client version: " + currentVersion + ", server client version: " + version);
// test url in lieu of having a configured server with a client-update available
//response.url = "http://localhost:8000/winimager/QtGui4.dll"
if(shouldUpdate(currentVersion, version)) {
updateSize = response.size;
// test metadata in lieu of having a configured server with a client-update available
// updateSize = 10000;
// version = "1.2.3"
if (confirm("The client will update to version " + version + " momentarily."))
{
startDownload(response.url)
}
}
},
error: function(jqXHR, textStatus, errorThrown) {
logger.error("Unable to do a client update check against /api/versioncheck");
}
});
}
function startDownload(url) {
logger.debug("starting client updater download from: " + url);
// initialize div in page so that we have something to update. temporary until real styles come
$('body').append($("<div id='downloadprogressbar_container'><span id='progressbar_info'>Downloading Client Update...<span id='progressbar_detail'></span></span><div id='downloadprogressbar'><div></div></div></div>"))
context.jamClient.ClientUpdateStartDownload(url,
"JK.ClientUpdate.DownloadProgressCallback",
"JK.ClientUpdate.DownloadSuccessCallback",
"JK.ClientUpdate.DownloadFailureCallback");
}
function startUpdate(updaterFilePath) {
logger.debug("starting client update from: " + updaterFilePath)
context.jamClient.ClientUpdateStartUpdate(updaterFilePath,
"JK.ClientUpdate.LaunchUpdateSuccessCallback",
"JK.ClientUpdate.LaunchUpdateFailureCallback");
}
function initialize() {
context.JK.ClientUpdate.DownloadProgressCallback = clientUpdateDownloadProgress;
context.JK.ClientUpdate.DownloadSuccessCallback = clientUpdateDownloadSuccess;
context.JK.ClientUpdate.DownloadFailureCallback = clientUpdateDownloadFailure;
context.JK.ClientUpdate.LaunchUpdateSuccessCallback = clientUpdateLaunchSuccess;
context.JK.ClientUpdate.LaunchUpdateFailureCallback = clientUpdateLaunchFailure;
return self;
}
// Expose publics
this.initialize = initialize;
this.check = check;
}
})(window,jQuery);