dlg_contentdb.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. --Minetest
  2. --Copyright (C) 2018-20 rubenwardy
  3. --
  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. --
  9. --This program is distributed in the hope that it will be useful,
  10. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. --GNU Lesser General Public License for more details.
  13. --
  14. --You should have received a copy of the GNU Lesser General Public License along
  15. --with this program; if not, write to the Free Software Foundation, Inc.,
  16. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. if not core.get_http_api then
  18. function create_contentdb_dlg()
  19. return messagebox("contentdb",
  20. fgettext("ContentDB is not available when Minetest was compiled without cURL"))
  21. end
  22. return
  23. end
  24. -- Filter
  25. local search_string = ""
  26. local cur_page = 1
  27. local num_per_page = 5
  28. local filter_type = 1
  29. local filter_types_titles = {
  30. fgettext("All packages"),
  31. fgettext("Games"),
  32. fgettext("Mods"),
  33. fgettext("Texture packs"),
  34. }
  35. -- Automatic package installation
  36. local auto_install_spec = nil
  37. local filter_types_type = {
  38. nil,
  39. "game",
  40. "mod",
  41. "txp",
  42. }
  43. local function install_or_update_package(this, package)
  44. local install_parent
  45. if package.type == "mod" then
  46. install_parent = core.get_modpath()
  47. elseif package.type == "game" then
  48. install_parent = core.get_gamepath()
  49. elseif package.type == "txp" then
  50. install_parent = core.get_texturepath()
  51. else
  52. error("Unknown package type: " .. package.type)
  53. end
  54. if package.queued or package.downloading then
  55. return
  56. end
  57. local function on_confirm()
  58. local has_hard_deps = contentdb.has_hard_deps(package)
  59. if has_hard_deps then
  60. local dlg = create_install_dialog(package)
  61. dlg:set_parent(this)
  62. this:hide()
  63. dlg:show()
  64. elseif has_hard_deps == nil then
  65. local dlg = messagebox("error_checking_deps",
  66. fgettext("Error getting dependencies for package"))
  67. dlg:set_parent(this)
  68. this:hide()
  69. dlg:show()
  70. else
  71. contentdb.queue_download(package, package.path and contentdb.REASON_UPDATE or contentdb.REASON_NEW)
  72. end
  73. end
  74. if package.type == "mod" and #pkgmgr.games == 0 then
  75. local dlg = messagebox("install_game",
  76. fgettext("You need to install a game before you can install a mod"))
  77. dlg:set_parent(this)
  78. this:hide()
  79. dlg:show()
  80. elseif not package.path and core.is_dir(install_parent .. DIR_DELIM .. package.name) then
  81. local dlg = create_confirm_overwrite(package, on_confirm)
  82. dlg:set_parent(this)
  83. this:hide()
  84. dlg:show()
  85. else
  86. on_confirm()
  87. end
  88. end
  89. -- Resolves the package specification stored in auto_install_spec into an actual package.
  90. -- May only be called after the package list has been loaded successfully.
  91. local function resolve_auto_install_spec()
  92. assert(contentdb.load_ok)
  93. if not auto_install_spec then
  94. return nil
  95. end
  96. local spec = contentdb.aliases[auto_install_spec] or auto_install_spec
  97. local resolved = nil
  98. for _, pkg in ipairs(contentdb.packages_full_unordered) do
  99. if pkg.id == spec then
  100. resolved = pkg
  101. break
  102. end
  103. end
  104. if not resolved then
  105. gamedata.errormessage = fgettext_ne("The package $1 was not found.", auto_install_spec)
  106. ui.update()
  107. auto_install_spec = nil
  108. end
  109. return resolved
  110. end
  111. -- Installs the package specified by auto_install_spec.
  112. -- Only does something if:
  113. -- a. The package list has been loaded successfully.
  114. -- b. The ContentDB dialog is currently visible.
  115. local function do_auto_install()
  116. if not contentdb.load_ok then
  117. return
  118. end
  119. local pkg = resolve_auto_install_spec()
  120. if not pkg then
  121. return
  122. end
  123. local contentdb_dlg = ui.find_by_name("contentdb")
  124. if not contentdb_dlg or contentdb_dlg.hidden then
  125. return
  126. end
  127. install_or_update_package(contentdb_dlg, pkg)
  128. auto_install_spec = nil
  129. end
  130. local function sort_and_filter_pkgs()
  131. contentdb.update_paths()
  132. contentdb.sort_packages()
  133. contentdb.filter_packages(search_string, filter_types_type[filter_type])
  134. local auto_install_pkg = resolve_auto_install_spec()
  135. if auto_install_pkg then
  136. local idx = table.indexof(contentdb.packages, auto_install_pkg)
  137. if idx ~= -1 then
  138. table.remove(contentdb.packages, idx)
  139. table.insert(contentdb.packages, 1, auto_install_pkg)
  140. end
  141. end
  142. end
  143. local function load()
  144. if contentdb.load_ok then
  145. sort_and_filter_pkgs()
  146. return
  147. end
  148. if contentdb.loading then
  149. return
  150. end
  151. contentdb.fetch_pkgs(function(result)
  152. if result then
  153. sort_and_filter_pkgs()
  154. do_auto_install()
  155. end
  156. ui.update()
  157. end)
  158. end
  159. local function get_info_formspec(text)
  160. local H = 9.5
  161. return table.concat({
  162. "formspec_version[6]",
  163. "size[15.75,9.5]",
  164. core.settings:get_bool("enable_touch") and "padding[0.01,0.01]" or "position[0.5,0.55]",
  165. "label[4,4.35;", text, "]",
  166. "container[0,", H - 0.8 - 0.375, "]",
  167. "button[0.375,0;5,0.8;back;", fgettext("Back to Main Menu"), "]",
  168. "container_end[]",
  169. })
  170. end
  171. local function get_formspec(dlgdata)
  172. if contentdb.loading then
  173. return get_info_formspec(fgettext("Loading..."))
  174. end
  175. if contentdb.load_error then
  176. return get_info_formspec(fgettext("No packages could be retrieved"))
  177. end
  178. assert(contentdb.load_ok)
  179. contentdb.update_paths()
  180. dlgdata.pagemax = math.max(math.ceil(#contentdb.packages / num_per_page), 1)
  181. if cur_page > dlgdata.pagemax then
  182. cur_page = 1
  183. end
  184. local W = 15.75
  185. local H = 9.5
  186. local formspec = {
  187. "formspec_version[6]",
  188. "size[15.75,9.5]",
  189. core.settings:get_bool("enable_touch") and "padding[0.01,0.01]" or "position[0.5,0.55]",
  190. "style[status,downloading,queued;border=false]",
  191. "container[0.375,0.375]",
  192. "field[0,0;7.225,0.8;search_string;;", core.formspec_escape(search_string), "]",
  193. "field_enter_after_edit[search_string;true]",
  194. "image_button[7.3,0;0.8,0.8;", core.formspec_escape(defaulttexturedir .. "search.png"), ";search;]",
  195. "image_button[8.125,0;0.8,0.8;", core.formspec_escape(defaulttexturedir .. "clear.png"), ";clear;]",
  196. "dropdown[9.175,0;2.7875,0.8;type;", table.concat(filter_types_titles, ","), ";", filter_type, "]",
  197. "container_end[]",
  198. -- Page nav buttons
  199. "container[0,", H - 0.8 - 0.375, "]",
  200. "button[0.375,0;5,0.8;back;", fgettext("Back to Main Menu"), "]",
  201. "container[", W - 0.375 - 0.8*4 - 2, ",0]",
  202. "image_button[0,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "start_icon.png;pstart;]",
  203. "image_button[0.8,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "prev_icon.png;pback;]",
  204. "style[pagenum;border=false]",
  205. "button[1.6,0;2,0.8;pagenum;", tonumber(cur_page), " / ", tonumber(dlgdata.pagemax), "]",
  206. "image_button[3.6,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "next_icon.png;pnext;]",
  207. "image_button[4.4,0;0.8,0.8;", core.formspec_escape(defaulttexturedir), "end_icon.png;pend;]",
  208. "container_end[]",
  209. "container_end[]",
  210. }
  211. if contentdb.number_downloading > 0 then
  212. formspec[#formspec + 1] = "button[12.5875,0.375;2.7875,0.8;downloading;"
  213. if #contentdb.download_queue > 0 then
  214. formspec[#formspec + 1] = fgettext("$1 downloading,\n$2 queued",
  215. contentdb.number_downloading, #contentdb.download_queue)
  216. else
  217. formspec[#formspec + 1] = fgettext("$1 downloading...", contentdb.number_downloading)
  218. end
  219. formspec[#formspec + 1] = "]"
  220. else
  221. local num_avail_updates = 0
  222. for i=1, #contentdb.packages_full do
  223. local package = contentdb.packages_full[i]
  224. if package.path and package.installed_release < package.release and
  225. not (package.downloading or package.queued) then
  226. num_avail_updates = num_avail_updates + 1
  227. end
  228. end
  229. if num_avail_updates == 0 then
  230. formspec[#formspec + 1] = "button[12.5875,0.375;2.7875,0.8;status;"
  231. formspec[#formspec + 1] = fgettext("No updates")
  232. formspec[#formspec + 1] = "]"
  233. else
  234. formspec[#formspec + 1] = "button[12.5875,0.375;2.7875,0.8;update_all;"
  235. formspec[#formspec + 1] = fgettext("Update All [$1]", num_avail_updates)
  236. formspec[#formspec + 1] = "]"
  237. end
  238. end
  239. if #contentdb.packages == 0 then
  240. formspec[#formspec + 1] = "label[4,4.75;"
  241. formspec[#formspec + 1] = fgettext("No results")
  242. formspec[#formspec + 1] = "]"
  243. end
  244. -- download/queued tooltips always have the same message
  245. local tooltip_colors = ";#dff6f5;#302c2e]"
  246. formspec[#formspec + 1] = "tooltip[downloading;" .. fgettext("Downloading...") .. tooltip_colors
  247. formspec[#formspec + 1] = "tooltip[queued;" .. fgettext("Queued") .. tooltip_colors
  248. local start_idx = (cur_page - 1) * num_per_page + 1
  249. for i=start_idx, math.min(#contentdb.packages, start_idx+num_per_page-1) do
  250. local package = contentdb.packages[i]
  251. local container_y = (i - start_idx) * 1.375 + (2*0.375 + 0.8)
  252. formspec[#formspec + 1] = "container[0.375,"
  253. formspec[#formspec + 1] = container_y
  254. formspec[#formspec + 1] = "]"
  255. -- image
  256. formspec[#formspec + 1] = "image[0,0;1.5,1;"
  257. formspec[#formspec + 1] = core.formspec_escape(get_screenshot(package))
  258. formspec[#formspec + 1] = "]"
  259. -- title
  260. formspec[#formspec + 1] = "label[1.875,0.1;"
  261. formspec[#formspec + 1] = core.formspec_escape(
  262. core.colorize(mt_color_green, package.title) ..
  263. core.colorize("#BFBFBF", " by " .. package.author))
  264. formspec[#formspec + 1] = "]"
  265. -- buttons
  266. local description_width = W - 2.625 - 2 * 0.7 - 2 * 0.15
  267. local second_base = "image_button[-1.55,0;0.7,0.7;" .. core.formspec_escape(defaulttexturedir)
  268. local third_base = "image_button[-2.4,0;0.7,0.7;" .. core.formspec_escape(defaulttexturedir)
  269. formspec[#formspec + 1] = "container["
  270. formspec[#formspec + 1] = W - 0.375*2
  271. formspec[#formspec + 1] = ",0.1]"
  272. if package.downloading then
  273. formspec[#formspec + 1] = "animated_image[-1.7,-0.15;1,1;downloading;"
  274. formspec[#formspec + 1] = core.formspec_escape(defaulttexturedir)
  275. formspec[#formspec + 1] = "cdb_downloading.png;3;400;]"
  276. elseif package.queued then
  277. formspec[#formspec + 1] = second_base
  278. formspec[#formspec + 1] = "cdb_queued.png;queued;]"
  279. elseif not package.path then
  280. local elem_name = "install_" .. i .. ";"
  281. formspec[#formspec + 1] = "style[" .. elem_name .. "bgcolor=#71aa34]"
  282. formspec[#formspec + 1] = second_base .. "cdb_add.png;" .. elem_name .. "]"
  283. formspec[#formspec + 1] = "tooltip[" .. elem_name .. fgettext("Install") .. tooltip_colors
  284. else
  285. if package.installed_release < package.release then
  286. -- The install_ action also handles updating
  287. local elem_name = "install_" .. i .. ";"
  288. formspec[#formspec + 1] = "style[" .. elem_name .. "bgcolor=#28ccdf]"
  289. formspec[#formspec + 1] = third_base .. "cdb_update.png;" .. elem_name .. "]"
  290. formspec[#formspec + 1] = "tooltip[" .. elem_name .. fgettext("Update") .. tooltip_colors
  291. description_width = description_width - 0.7 - 0.15
  292. end
  293. local elem_name = "uninstall_" .. i .. ";"
  294. formspec[#formspec + 1] = "style[" .. elem_name .. "bgcolor=#a93b3b]"
  295. formspec[#formspec + 1] = second_base .. "cdb_clear.png;" .. elem_name .. "]"
  296. formspec[#formspec + 1] = "tooltip[" .. elem_name .. fgettext("Uninstall") .. tooltip_colors
  297. end
  298. local web_elem_name = "view_" .. i .. ";"
  299. formspec[#formspec + 1] = "image_button[-0.7,0;0.7,0.7;" ..
  300. core.formspec_escape(defaulttexturedir) .. "cdb_viewonline.png;" .. web_elem_name .. "]"
  301. formspec[#formspec + 1] = "tooltip[" .. web_elem_name ..
  302. fgettext("View more information in a web browser") .. tooltip_colors
  303. formspec[#formspec + 1] = "container_end[]"
  304. -- description
  305. formspec[#formspec + 1] = "textarea[1.855,0.3;"
  306. formspec[#formspec + 1] = tostring(description_width)
  307. formspec[#formspec + 1] = ",0.8;;;"
  308. formspec[#formspec + 1] = core.formspec_escape(package.short_description)
  309. formspec[#formspec + 1] = "]"
  310. formspec[#formspec + 1] = "container_end[]"
  311. end
  312. return table.concat(formspec)
  313. end
  314. local function handle_submit(this, fields)
  315. if fields.search or fields.key_enter_field == "search_string" then
  316. search_string = fields.search_string:trim()
  317. cur_page = 1
  318. contentdb.filter_packages(search_string, filter_types_type[filter_type])
  319. return true
  320. end
  321. if fields.clear then
  322. search_string = ""
  323. cur_page = 1
  324. contentdb.filter_packages("", filter_types_type[filter_type])
  325. return true
  326. end
  327. if fields.back then
  328. this:delete()
  329. return true
  330. end
  331. if fields.pstart then
  332. cur_page = 1
  333. return true
  334. end
  335. if fields.pend then
  336. cur_page = this.data.pagemax
  337. return true
  338. end
  339. if fields.pnext then
  340. cur_page = cur_page + 1
  341. if cur_page > this.data.pagemax then
  342. cur_page = 1
  343. end
  344. return true
  345. end
  346. if fields.pback then
  347. if cur_page == 1 then
  348. cur_page = this.data.pagemax
  349. else
  350. cur_page = cur_page - 1
  351. end
  352. return true
  353. end
  354. if fields.type then
  355. local new_type = table.indexof(filter_types_titles, fields.type)
  356. if new_type ~= filter_type then
  357. filter_type = new_type
  358. cur_page = 1
  359. contentdb.filter_packages(search_string, filter_types_type[filter_type])
  360. return true
  361. end
  362. end
  363. if fields.update_all then
  364. for i=1, #contentdb.packages_full do
  365. local package = contentdb.packages_full[i]
  366. if package.path and package.installed_release < package.release and
  367. not (package.downloading or package.queued) then
  368. contentdb.queue_download(package, contentdb.REASON_UPDATE)
  369. end
  370. end
  371. return true
  372. end
  373. local start_idx = (cur_page - 1) * num_per_page + 1
  374. assert(start_idx ~= nil)
  375. for i=start_idx, math.min(#contentdb.packages, start_idx+num_per_page-1) do
  376. local package = contentdb.packages[i]
  377. assert(package)
  378. if fields["install_" .. i] then
  379. install_or_update_package(this, package)
  380. return true
  381. end
  382. if fields["uninstall_" .. i] then
  383. local dlg = create_delete_content_dlg(package)
  384. dlg:set_parent(this)
  385. this:hide()
  386. dlg:show()
  387. return true
  388. end
  389. if fields["view_" .. i] then
  390. local url = ("%s/packages/%s?protocol_version=%d"):format(
  391. core.settings:get("contentdb_url"), package.url_part,
  392. core.get_max_supp_proto())
  393. core.open_url(url)
  394. return true
  395. end
  396. end
  397. return false
  398. end
  399. local function handle_events(event)
  400. if event == "DialogShow" then
  401. -- On touchscreen, don't show the "MINETEST" header behind the dialog.
  402. mm_game_theme.set_engine(core.settings:get_bool("enable_touch"))
  403. -- If ContentDB is already loaded, auto-install packages here.
  404. do_auto_install()
  405. return true
  406. end
  407. return false
  408. end
  409. --- Creates a ContentDB dialog.
  410. ---
  411. --- @param type string | nil
  412. --- Sets initial package filter. "game", "mod", "txp" or nil (no filter).
  413. --- @param install_spec table | nil
  414. --- ContentDB ID of package as returned by pkgmgr.get_contentdb_id().
  415. --- Sets package to install or update automatically.
  416. function create_contentdb_dlg(type, install_spec)
  417. search_string = ""
  418. cur_page = 1
  419. if type then
  420. -- table.indexof does not work on tables that contain `nil`
  421. for i, v in pairs(filter_types_type) do
  422. if v == type then
  423. filter_type = i
  424. break
  425. end
  426. end
  427. else
  428. filter_type = 1
  429. end
  430. -- Keep the old auto_install_spec if the caller doesn't specify one.
  431. if install_spec then
  432. auto_install_spec = install_spec
  433. end
  434. load()
  435. return dialog_create("contentdb",
  436. get_formspec,
  437. handle_submit,
  438. handle_events)
  439. end