Move values the mainmenu caches to dedicated files (#14433)
[minetest.git] / builtin / mainmenu / dlg_version_info.lua
blob483cd30bd100b2145c7e26e72c82c7d655ba3317
1 --[[
2 Minetest
3 Copyright (C) 2018-2020 SmallJoker, 2022 rubenwardy
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 if not core.get_http_api then
21 function check_new_version()
22 end
23 return
24 end
26 local function version_info_formspec(data)
27 local cur_ver = core.get_version()
28 local title = fgettext("A new $1 version is available", cur_ver.project)
29 local message =
30 fgettext("Installed version: $1\nNew version: $2\n" ..
31 "Visit $3 to find out how to get the newest version and stay up to date" ..
32 " with features and bugfixes.",
33 cur_ver.string, data.new_version or "", data.url or "")
35 local fs = {
36 "formspec_version[3]",
37 "size[12.8,7]",
38 "style_type[label;textcolor=#0E0]",
39 "label[0.5,0.8;", title, "]",
40 "textarea[0.4,1.6;12,3.4;;;", message, "]",
41 "container[0.4,5.8]",
42 "button[0.0,0;4.0,0.8;version_check_visit;", fgettext("Visit website"), "]",
43 "button[4.5,0;3.5,0.8;version_check_remind;", fgettext("Later"), "]",
44 "button[8.5.5,0;3.5,0.8;version_check_never;", fgettext("Never"), "]",
45 "container_end[]",
48 return table.concat(fs, "")
49 end
51 local function version_info_buttonhandler(this, fields)
52 if fields.version_check_remind then
53 -- Erase last known, user will be reminded again at next check
54 cache_settings:set("update_last_known", "")
55 this:delete()
56 return true
57 end
58 if fields.version_check_never then
59 -- clear checked URL
60 core.settings:set("update_information_url", "")
61 this:delete()
62 return true
63 end
64 if fields.version_check_visit then
65 if type(this.data.url) == "string" then
66 core.open_url(this.data.url)
67 end
68 this:delete()
69 return true
70 end
72 return false
73 end
75 local function version_info_eventhandler(event)
76 if event == "DialogShow" then
77 mm_game_theme.set_engine()
78 return true
79 end
81 return false
82 end
84 local function create_version_info_dlg(new_version, url)
85 assert(type(new_version) == "string")
86 assert(type(url) == "string")
88 local retval = dialog_create("version_info",
89 version_info_formspec,
90 version_info_buttonhandler,
91 version_info_eventhandler)
93 retval.data.new_version = new_version
94 retval.data.url = url
96 return retval
97 end
99 local function get_current_version_code()
100 -- Format: Major.Minor.Patch
101 -- Convert to MMMNNNPPP
102 local cur_string = core.get_version().string
103 local cur_major, cur_minor, cur_patch = cur_string:match("^(%d+).(%d+).(%d+)")
105 if not cur_patch then
106 core.log("error", "Failed to parse version numbers (invalid tag format?)")
107 return
110 return (cur_major * 1000 + cur_minor) * 1000 + cur_patch
113 local function on_version_info_received(json)
114 local maintab = ui.find_by_name("maintab")
115 if maintab.hidden then
116 -- Another dialog is open, abort.
117 return
120 local known_update = tonumber(cache_settings:get("update_last_known")) or 0
122 -- Format: MMNNPPP (Major, Minor, Patch)
123 local new_number = type(json.latest) == "table" and json.latest.version_code
124 if type(new_number) ~= "number" then
125 core.log("error", "Failed to read version number (invalid response?)")
126 return
129 local cur_number = get_current_version_code()
130 if new_number <= known_update or new_number < cur_number then
131 return
134 -- Also consider updating from 1.2.3-dev to 1.2.3
135 if new_number == cur_number and not core.get_version().is_dev then
136 return
139 cache_settings:set("update_last_known", tostring(new_number))
141 -- Show version info dialog (once)
142 maintab:hide()
144 local version_info_dlg = create_version_info_dlg(json.latest.version, json.latest.url)
145 version_info_dlg:set_parent(maintab)
146 version_info_dlg:show()
148 ui.update()
151 function check_new_version()
152 local url = core.settings:get("update_information_url")
153 if url == "" then
154 -- Never show any updates
155 return
158 -- every 2 days
159 if check_cache_age("update_last_checked", 2 * 24 * 3600) then
160 return
162 cache_settings:set("update_last_checked", tostring(os.time()))
164 -- Clean old leftovers (this can be removed after 5.9.0 or so)
165 core.settings:remove("update_last_checked")
166 core.settings:remove("update_last_known")
168 core.handle_async(function(params)
169 local http = core.get_http_api()
170 return http.fetch_sync(params)
171 end, { url = url }, function(result)
172 local json = result.succeeded and core.parse_json(result.data)
173 if type(json) ~= "table" or not json.latest then
174 core.log("error", "Failed to read JSON output from " .. url ..
175 ", status code = " .. result.code)
176 return
179 on_version_info_received(json)
180 end)