dlg_version_info.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. --[[
  2. Minetest
  3. Copyright (C) 2018-2020 SmallJoker, 2022 rubenwardy
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. ]]
  16. if not core.get_http_api then
  17. function check_new_version()
  18. end
  19. return
  20. end
  21. local function version_info_formspec(data)
  22. local cur_ver = core.get_version()
  23. local title = fgettext("A new $1 version is available", cur_ver.project)
  24. local message =
  25. fgettext("Installed version: $1\nNew version: $2\n" ..
  26. "Visit $3 to find out how to get the newest version and stay up to date" ..
  27. " with features and bugfixes.",
  28. cur_ver.string, data.new_version or "", data.url or "")
  29. local fs = {
  30. "formspec_version[3]",
  31. "size[12.8,7]",
  32. "style_type[label;textcolor=#0E0]",
  33. "label[0.5,0.8;", title, "]",
  34. "textarea[0.4,1.6;12,3.4;;;", message, "]",
  35. "container[0.4,5.8]",
  36. "button[0.0,0;4.0,0.8;version_check_visit;", fgettext("Visit website"), "]",
  37. "button[4.5,0;3.5,0.8;version_check_remind;", fgettext("Later"), "]",
  38. "button[8.5.5,0;3.5,0.8;version_check_never;", fgettext("Never"), "]",
  39. "container_end[]",
  40. }
  41. return table.concat(fs, "")
  42. end
  43. local function version_info_buttonhandler(this, fields)
  44. if fields.version_check_remind then
  45. -- Erase last known, user will be reminded again at next check
  46. core.settings:set("update_last_known", "")
  47. this:delete()
  48. return true
  49. end
  50. if fields.version_check_never then
  51. core.settings:set("update_last_checked", "disabled")
  52. this:delete()
  53. return true
  54. end
  55. if fields.version_check_visit then
  56. if type(this.data.url) == "string" then
  57. core.open_url(this.data.url)
  58. end
  59. this:delete()
  60. return true
  61. end
  62. return false
  63. end
  64. local function create_version_info_dlg(new_version, url)
  65. assert(type(new_version) == "string")
  66. assert(type(url) == "string")
  67. local retval = dialog_create("version_info",
  68. version_info_formspec,
  69. version_info_buttonhandler,
  70. nil)
  71. retval.data.new_version = new_version
  72. retval.data.url = url
  73. return retval
  74. end
  75. local function get_current_version_code()
  76. -- Format: Major.Minor.Patch
  77. -- Convert to MMMNNNPPP
  78. local cur_string = core.get_version().string
  79. local cur_major, cur_minor, cur_patch = cur_string:match("^(%d+).(%d+).(%d+)")
  80. if not cur_patch then
  81. core.log("error", "Failed to parse version numbers (invalid tag format?)")
  82. return
  83. end
  84. return (cur_major * 1000 + cur_minor) * 1000 + cur_patch
  85. end
  86. local function on_version_info_received(json)
  87. local maintab = ui.find_by_name("maintab")
  88. if maintab.hidden then
  89. -- Another dialog is open, abort.
  90. return
  91. end
  92. local known_update = tonumber(core.settings:get("update_last_known")) or 0
  93. -- Format: MMNNPPP (Major, Minor, Patch)
  94. local new_number = type(json.latest) == "table" and json.latest.version_code
  95. if type(new_number) ~= "number" then
  96. core.log("error", "Failed to read version number (invalid response?)")
  97. return
  98. end
  99. local cur_number = get_current_version_code()
  100. if new_number <= known_update or new_number < cur_number then
  101. return
  102. end
  103. -- Also consider updating from 1.2.3-dev to 1.2.3
  104. if new_number == cur_number and not core.get_version().is_dev then
  105. return
  106. end
  107. core.settings:set("update_last_known", tostring(new_number))
  108. -- Show version info dialog (once)
  109. maintab:hide()
  110. local version_info_dlg = create_version_info_dlg(json.latest.version, json.latest.url)
  111. version_info_dlg:set_parent(maintab)
  112. version_info_dlg:show()
  113. ui.update()
  114. end
  115. function check_new_version()
  116. local url = core.settings:get("update_information_url")
  117. if core.settings:get("update_last_checked") == "disabled" or
  118. url == "" then
  119. -- Never show any updates
  120. return
  121. end
  122. local time_now = os.time()
  123. local time_checked = tonumber(core.settings:get("update_last_checked")) or 0
  124. if time_now - time_checked < 2 * 24 * 3600 then
  125. -- Check interval of 2 entire days
  126. return
  127. end
  128. core.settings:set("update_last_checked", tostring(time_now))
  129. core.handle_async(function(params)
  130. local http = core.get_http_api()
  131. return http.fetch_sync(params)
  132. end, { url = url }, function(result)
  133. local json = result.succeeded and core.parse_json(result.data)
  134. if type(json) ~= "table" or not json.latest then
  135. core.log("error", "Failed to read JSON output from " .. url ..
  136. ", status code = " .. result.code)
  137. return
  138. end
  139. on_version_info_received(json)
  140. end)
  141. end