upgrade.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. JqueryClass('upgradeWindow', {
  4. init: function (options) {
  5. var self = $(this)
  6. options = $.extend({
  7. icon: $('<div>'),
  8. windowManager: $('<div>'),
  9. startUpgrade: function (callback) {
  10. callback(true)
  11. },
  12. startDeviceUpgrade: function () {},
  13. }, options)
  14. self.data(options)
  15. self.data('updatedata', null)
  16. self.data('updaterequired', false)
  17. self.data('updatesystem', false)
  18. self.data('eggshown', false)
  19. options.icon.statusTooltip()
  20. options.icon.statusTooltip('message', 'Checking for updates...', true)
  21. options.icon.click(function () {
  22. self.upgradeWindow('open')
  23. })
  24. $('body').keydown(function (e) {
  25. if (e.keyCode == 27)
  26. self.upgradeWindow('close')
  27. })
  28. self.find('.js-close').click(function () {
  29. self.upgradeWindow('close')
  30. })
  31. self.find('button.js-upgrade').click(function () {
  32. if ($(this).hasClass('disabled')) {
  33. return
  34. }
  35. if ($(this).text() == "Upgrade Now") {
  36. if (self.data('updatesystem')) {
  37. self.upgradeWindow('startUpgrade')
  38. } else {
  39. self.upgradeWindow('startDeviceUpgrade')
  40. }
  41. } else {
  42. self.upgradeWindow('downloadStart')
  43. }
  44. })
  45. self.hide()
  46. return self
  47. },
  48. open: function () {
  49. var self = $(this)
  50. var data = self.data('updatedata')
  51. if (! data) {
  52. return
  53. }
  54. var html
  55. if (self.data('updatesystem')) {
  56. html = "Update version <b>" + data['version'].replace("v","") + "</b>.<br/>" +
  57. "Released on " + data['release-date'].split('T')[0] + ".";
  58. if (window.location.host == "192.168.50.1") {
  59. html += "<br/><br/>" +
  60. "Sorry, cannot update via bluetooth.<br/>" +
  61. "Please connect the MOD via USB and try again.";
  62. self.find('button.js-upgrade').addClass('disabled')
  63. } else if (self.data('updaterequired')) {
  64. html += "<br/><br/>" +
  65. "<b>This update is required!</b>";
  66. }
  67. } else {
  68. html = "One of your connected Control Chain devices is using outdated firmware, please update.<br/>"
  69. "Just follow the instructions on <a href='#' target='_blank'>this link</a>.";
  70. }
  71. var p = self.find('.mod-upgrade-details').find('p')
  72. $(p[0]).html(html)
  73. self.find('a').attr('href', data['release-url'])
  74. self.find('button.js-upgrade').text("Download")
  75. self.show()
  76. },
  77. close: function () {
  78. var self = $(this)
  79. self.hide()
  80. if (self.data('updatesystem')) {
  81. setCookie("auto-updated-canceled_" + VERSION, "true", 15)
  82. } else {
  83. setCookie("device-update-canceled_" + VERSION, "true", 15)
  84. }
  85. },
  86. setup: function (required, data) {
  87. var self = $(this)
  88. var icon = self.data('icon')
  89. var ignoreUpdate = (getCookie("auto-updated-canceled_" + VERSION, "false") == "true")
  90. self.data('updatedata', data)
  91. self.data('updaterequired', false)
  92. self.data('updatesystem', true)
  93. icon.statusTooltip('message', "An update is available, click to learn more", ignoreUpdate || required, 8000)
  94. icon.statusTooltip('status', 'update-available')
  95. if (required && ! ignoreUpdate) {
  96. self.upgradeWindow('open')
  97. new Notification('warn', 'A required update is available.<br/>Please update.', 8000)
  98. }
  99. },
  100. setupDevice: function (data) {
  101. var self = $(this)
  102. if (self.data('updatesystem')) {
  103. return
  104. }
  105. var icon = self.data('icon')
  106. var ignoreUpdate = (getCookie("device-update-canceled_" + VERSION, "false") == "true")
  107. self.data('updatedata', data)
  108. self.data('updaterequired', false)
  109. self.data('updatesystem', false)
  110. icon.statusTooltip('message', "A device update is available, click to learn more", ignoreUpdate, 8000)
  111. icon.statusTooltip('status', 'update-available')
  112. },
  113. cancelDeviceSetup: function (dev_uri) {
  114. var self = $(this)
  115. if (self.data('updatesystem')) {
  116. return
  117. }
  118. var data = self.data('updatedata')
  119. if (! data) {
  120. return
  121. }
  122. var uri = data['uri']
  123. if (! uri || uri != dev_uri) {
  124. return
  125. }
  126. self.hide()
  127. self.upgradeWindow('setUpdated')
  128. },
  129. setErrored: function () {
  130. var self = $(this)
  131. var icon = self.data('icon')
  132. icon.statusTooltip('message', "Failed to connect to MOD Cloud", true)
  133. icon.statusTooltip('status', 'error')
  134. },
  135. setUpdated: function () {
  136. var self = $(this)
  137. self.data('updatedata', null)
  138. var icon = self.data('icon')
  139. icon.statusTooltip('message', "System is up-to-date", true)
  140. icon.statusTooltip('status', 'uptodate')
  141. var date = new Date()
  142. var d = date.getDay(),
  143. m = date.getMonth();
  144. if (m == 12 && (d == 24 || d == 25) && ! self.data('eggshown')) {
  145. self.data('eggshown', true)
  146. setTimeout(function() {
  147. new Notification('warn', 'The MOD Team wishes you happy holidays!', 8000)
  148. }, 5000)
  149. }
  150. },
  151. downloadStart: function () {
  152. var self = $(this)
  153. self.find('.mod-upgrade-details').hide()
  154. self.find('.download-progress').show()
  155. self.find('.progressbar').width(0)
  156. self.find('.download-start').show().text("Downloading...")
  157. self.find('.download-complete').hide()
  158. var transfer = new SimpleTransference(self.data('updatedata')['download-url'],
  159. self.data('updatesystem') ? '/update/download' : '/controlchain/download')
  160. transfer.reportPercentageStatus = function (percentage) {
  161. self.find('.progressbar').width(self.find('.progressbar-wrapper').width() * percentage)
  162. if (percentage == 1) {
  163. self.find('.download-start').text("Preparing update... (may take a few minutes)")
  164. }
  165. }
  166. transfer.reportFinished = function (resp) {
  167. self.find('.mod-upgrade-details').show().find('p:lt(2)').show()
  168. self.find('.download-progress').hide()
  169. self.find('button.js-upgrade').text("Upgrade Now")
  170. self.find('.download-start').hide()
  171. self.find('.download-complete').show()
  172. if (self.data('updatesystem')) {
  173. if (confirm("The MOD will now be updated. Any unsaved work will be lost. "+
  174. "The upgrade can take several minutes, "+
  175. "in which you may not be able to play or do anything else. Continue?")) {
  176. self.upgradeWindow('startUpgrade')
  177. }
  178. } else {
  179. self.upgradeWindow('startDeviceUpgrade')
  180. }
  181. }
  182. transfer.reportError = function (error) {
  183. self.find('.mod-upgrade-details').show().find('p:lt(2)').hide()
  184. self.find('button.js-upgrade').text("Retry")
  185. self.find('.download-start').show().text("Download failed!")
  186. self.find('.download-complete').hide()
  187. }
  188. transfer.start()
  189. },
  190. startUpgrade: function () {
  191. var self = $(this)
  192. self.data('startUpgrade')(function (ok) {
  193. if (ok) {
  194. desktop.blockUI(true)
  195. } else {
  196. new Bug("Failed to start upgrade")
  197. }
  198. })
  199. },
  200. startDeviceUpgrade: function () {
  201. var self = $(this)
  202. self.data('startDeviceUpgrade')()
  203. },
  204. })